Table of contents
1.
Introduction
2.
Creating An App Model
3.
What Are Migrations?
4.
Makemigrations
5.
Migrate
6.
Frequently Asked Questions
7.
Key Takeaways
Last Updated: Mar 27, 2024

Django Basic App Model

Author Rajat Agrawal
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Model in Django is the single, definitive source of information about the data. It contains the essential fields and behaviors of the data you are storing.

Generally, each model maps to a single database table.

Django gives us an auto-generated database access API. It provides a built-in database by default that is the SQLite database.

Let’s see how to create an app model in Django. 

Creating An App Model

Let's say we are creating a project codingninjas in which we are creating an app ninjas.

To create the app codingninjas, we will run the following command:

django-admin startproject codingninjas .
You can also try this code with Online Python Compiler
Run Code


After creating the project codingninjas, the project structure will look like this:


To create the app ninjas, we will run the following command:

python manage.py startapp ninjas
You can also try this code with Online Python Compiler
Run Code


After creating the app, now the project folder will look like this:

Since our project does not know what apps we have created. It’s important to add the app name(ninjas here) into the INSTALLED_APPS list in settings.py of our project (codingninjas here).

In the picture below, we have added ninjas to the INSTALLED_APPS list in the settings.py file.

Now we have successfully created our app. Let's now move on to create the first model in the models.py file of our app. 

Initially, the models.py file will look like this:

Django provides a set of predefined methods and fields to create a model.

In the models.py file, we need to create our own model class. Our own model class will inherit Python’s model class.

Syntax: 

class ClassName(models.Model):
field_name = models.field_type(arg,options)
You can also try this code with Online Python Compiler
Run Code


For example:

class Students(models.Model):
    Id = models.IntegerField(primary_key='true')
    Name = models.CharField(max_length=25)
    Department = models.CharField(max_length=25)
    Email = models.EmailField(max_length=30)
You can also try this code with Online Python Compiler
Run Code


There are some relations between the model class and SQL query: 

1.) The Student class will create a table with columns and their data types.

2.) Table Name will be AppName_ClassName (ninjas_Student here).

3.) Column name will be the field_name (e.g., Id, Name, Department, Email are the column names here).

4.) If we do not assign the primary_key attribute to any fields, Python will automatically generate a field_name id with a datatype integer with primary_key and auto-increment.

After making any changes to models.py, we need to run the following command: 

python manage.py makemigrations
You can also try this code with Online Python Compiler
Run Code


The makemigrations command will convert the model class into an SQL query.

And finally, to implement the database changes, we need to run the following command:

python manage.py migrate
You can also try this code with Online Python Compiler
Run Code


The migrate command will help in executing the SQL query.

After running the above two commands in order, finally, a new database table would have been created in the migrations folder of our app folder.

We can see that the 0001_intial.py file has been created in the migrations folder of our app folder ninjas.

ninjas->migrations->0001_inital.py

We have successfully created our model app.

Now, let’s dive deeper into the Migrations concept and understand the migrations in Django.

What Are Migrations?

Migrations are the Django way of propagating changes you make to your models(updating a model, deleting a model, etc.) into the database schema.

Django provides four basic commands to make migrations:

1.) makemigrations: It is responsible for creating new migrations based on the changes you have made to your models. It basically converts the model class into a SQL query or statement.

2.) migrate: It is responsible for applying and unapplying migrations. It basically executes the SQL query or statement.

3.) sqlmigrate: It will display the SQL statements for a migration.

4.) showmigrationsIt will list a project's migration and its status.

While working with Django models, the makemigrations and migrate commands are the most important and frequently used commands.

Let’s discuss both these commands in-depth.

Makemigrations

makemigrations is used to convert the model class into an SQL statement. This will also create a file that contains the SQL statements. This file is present in our application’s migrations folder.

Command:

python manage.py makemigrations
You can also try this code with Online Python Compiler
Run Code

Let’s try to understand the example we took earlier in the creating the model section.

After running the makemigrations command, we can see that the 0001_intial.py file has been created in the migrations folder of the app folder ninjas.

ninjas->migrations->0001_inital.py

Whenever we make any changes to the models.py file, like changing the field_name, field_type, etc., a new file will be created in the migrations folder.

We have added a new field (PhoneNumber) having filed_type = IntegerField and default=0 in the Students model class. 

PhoneNumber = models.CharField(max_length=12, default = 0)
You can also try this code with Online Python Compiler
Run Code

After that, we run the makemigrations command, and a new file with 0002_students_phonenumber.py has been created in the migrations folder. We can see this in the picture below.

The dependencies and operations lists are now changed.

The dependencies list is showing the dependency of this file on 0001_initial.py.

The operations list is showing the operation we performed in our actual model class.

Migrate

The migrate is used to execute SQL statements generated by makemigrations. This command will execute all the Application (including built-in applications) SQL statements if available. After executing the SQL statement, the Table is created in the Database. 

Command:

python manage.py migrate
You can also try this code with Online Python Compiler
Run Code

Note: If you are making any changes in your own model class, you must run makemigrations and migrate commands, respectively; only then will all those changes be reflected in your application.

Always, migrate command to be run after makemigrations to execute all those changes in the database table.

Frequently Asked Questions

1.) How to create a new project in Django?

Ans. To create a new project in Django, run the following command:

django-admin startproject <project_name>
You can also try this code with Online Python Compiler
Run Code


2.) How to create a new application in Django?

Ans. To create a new application in Django, run the following command:

python manage.py startapp ninjas
You can also try this code with Online Python Compiler
Run Code

 

3.) Difference between makemigrations and migrate command?

Ans. makemigrations is used to create SQL queries for the model class while migrate command is used to execute the SQL query generated by the makemigrations command.

Key Takeaways

In this article, we learned how to create a model in Django, the concept of migration, and the use of makemigrations & migrate commands.

You can also refer to other exciting blogs related to Django like Django TemplatesDjango Forms, and CRUD operations in Django.

Happy Learning!!

Live masterclass