How to create Django Model Initialization?
Database designing Part 2
Hello programmers, As we mentioned before. This post is getting a bit lengthy so we have divided this into two parts. So before getting started for this section we will take a recap of what we have done in previous session.
Recap
In previous session, We have discussed about Model one of the essential part of model web development. we have discussed what is model ? importance of Model, How Django treats model using python, How to initialize Model in Django and how to design Database for our web application. We have executed some commands for Model initialization as we as coded few things to the .py file. You can get this stuff Here
Now in this session we are going to perform next step towards our project so lets start for this.
We will continue where we left i.e. After entering makemigration command to create the table in our database Now enter following command to open python shell and to interact with our application.
python3 manage.py shell
It will open a python shell which is a bit different from our regular python shell. The difference is in this shell our project path is automatically get added into system path and the second difference is to hold our settings.py a special variable is get created. So whenever you want to interact with your project use this command to open python shell.
Now we will code for something.
After executing above command, enter following command to import contents of our model
>>> from bookmarks.models import *
Now we are going to add a sample link into our database to do so we are going to create an instance of our Link Class which we have declared in our model.py class. and don’t forget to call the save method to save the link into database
>>> link1 = Link(url=’http://www.bitsolve.in/’)
>>> link1.save()
>>> link2 = Link(url=’http://www.youtube.com/’)
>>> link2.save()
Calling the save() method is important as the object is temporarily get stored into memory and will get deleted once you close the terminal. By calling the save() method we are going to store the object value into Database.
To examine and change the url value enter the following command
>>> link2.url
>>> ‘http://www.youtube.com/’
>>> link2.url = ‘http://www.facebook.com/’
>>> link2.save()
To get list of all links stored in our database enter the following command
>>> links = Link.objects.all()
>>> for link in links:
… print link.url
…
http://www.bitsolve.in/
http://www.facebook.com/
And at the end to delete particular link enter the following command
>>> link2.delete()
>>> Link.object.count()
1
Here the output i.e count 1 may vary as you will store multiple links and or delete multiple links from the database. It is just showing the remaining link objects from the database.
The User Data Model
Now we have clear idea about Data models, so lets move to user Data model. There is a plue point for us using Django as Django comes with user data model ready for use. It makes us easy management of user accounts. This model is associated with User account details such as user name, password and email address. This model User is located in django.contrib.auth.models package
So firstly we are going to create a superuser for our project to do so firstly exit from python shell by simply entering exit() or pression Ctrl + Z. Or open new cmd window and enter following command to create super user.
python3 manage.py createsuperuser
This command will open window like this
Here you have to enter your desired username (leave blank if you want username same as system name),your email id and password for selected username. Now move back to our python shell and enter following command to get user accounts in our database
Here we can see that our superuser or admin is by default user of our database.
The Bookmark Data Model
At last but not the least we have remaining one data model that is Bookmark Data model. which we are going to use to connect bookmarks to user account. A bookmark is entity which belongs to one user and one link. Some times many user bookmarks one link or many links are get bookmarked by one user. In programming language we called this as many to many relationship.
Now the following code contains the Bookmark data model. we must write this code into our models. py file which will look like this
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Link(models.Model):
url = models.URLField(unique=True)
class Bookmark(models.Model):
title = models.CharField(maxlength=200)
user = models.ForeignKey(user)
link = models.ForeignKey(Link)
Here we have first import the User class in order to refer it to Bookmark model. After this we have declared a Link class as we have defined earlier for Link. Now this new model consist of a text field which contains two foreignkeys for User and Link model .
After all this run the following commands to create its corresponding table and save the changes into application we have made this far.
python3 manage.py makemigrations
python3 manage.py migrate
And you will see something like this
So here we stop for now. In our next post we will look for another major component of Django i.e. The templte system. Till now if you face any problem regarding this post. Feel free to comment below. we will try to solve your problem as soon as possible. If you like this post and any other post on this website feel free to share this with your friends.
Thank you..
Happy Coding..