So , we are done with the basics of Django and we can now take a step forward and work with the databases to store and retrieve our data. We will be storing the data into our database/table by using models.py which was created at the time of creation of our application. Each model works like a table having fields and values. Today, we will be creating a simple database which will store the name and surname of a person. Right now, we will be storing the data explicitly into our database, but later on we will be working on the back-end automatic process. Let’s start!!!
Our previous projects can work (actually worked) without adding our application to the INSTALLED_APPS, but for this we have to add our application to the list. I have already created a project and application named PRO and APP respectively. I have added the APP as shown:
Models are classes which contains the variables as its members. They are actually derived from models.Model class, which is available in django.db:
from django.db import models
Here “basicinfo” is the derived class and “models.Model” is the base class. It has lot of inbuilt functions, but for now we will stick to only its character field as we want to input name only. We can even provide some constraints like the length of the name, default value, null value, unique values etc. We are using two variables to store the first and last name:
As we discussed at the start of our Django journey about migrations, now we will be actually studying about it. Migration is done basically so as to save changes to our databases which we have created. It helps in easy importing of databases from one system to another, you can get more details about migrations by reading the documentation (https://docs.djangoproject.com/en/2.0/topics/migrations/). Now, after creating our models, we have to use migrate command to tell the Django about our changes by using python manage.py migrate command:
We have to now apply this to our application by using:
python manage.py makemigrations APP
After this again we have to run the migration command to take care of the changes:
So now we have a table named “basicinfo” with two columns as first and last for adding first name and last name respectively. There are two ways of adding the data into our table. First we will learn the tedious way of adding with the help of shell and in the next blog we will learn about the admin panel of Django. In order to open python shell in anaconda just enter:
python manage.py shell
This will open the python console where we can enter our data. First, we have to get our table using import command. After importing we have to create an object for the class (similar to constructor in C++). The arguments will be our column names along with the data to enter. After entering the data, we have to save the data using object.save(). We can also check the number of available objects for a class using:
The above function will generate an list of all objects created. We can check whether data is successfully added by accessing the members with the help of objects using object.column_name. All the above specified work is done below:
So, this was the long way to highway, we will be working with the shorter way in our next blog. We will learn to create a superuser and make use of our admin panel in the next blog, till then keep coding…
Thank you for reading, if you liked the article please click on the clap button and show your support. You can clap more than once, just keep pressing the icon!!!