In this tutorial, we’ll get into the details of Django Models. Before we do that, let’s quickly set up our Django Project from the terminal.


Once we’ve created the school app, we need to add it in the INSTALLED_APPS inside the


Take note of the file that’s created in the school app. It’ll be the Most Wanted file in this tutorial. Let’s get started.

Django Models

The database is an important part of any web application. Django Models are a source of information for your data. You can store values in the form of fields in it.

Each Django Model would be held in the database and the fields defined in the Django Models are database fields.

Django Models are used to execute SQL code behind the scene as well.

Let’s create our first Django Model in the file.

Django Model Example

Following is the code from the file:

Student is a Django Model with four fields.

Django Model Fields

Django Models can have any of the following types of fields CharField, IntegerField, EmailField, URLField, DateField, BooleanField, AutoField, UUIDField, OneToOneField, ManyToOneField.

How to set a primary key?

Field Options

max_length – Set the maximum number of characters allowed

verbose_name – human readable name of the field

null – Setting null equal to True allows us to leave the column as blank. The column would then store NULL as the value.


blank – Again the field is allowed blank if blank=True. blank is used for Form validations where we can specify if entering in a field is mandatory or not.

choices – This is like a 2-tuple and is used to choose from a list of values.

default field option is required in choices unless to put a null or blank option.

How to add this Model to the Database?

The above command creates the model.

This is the output from the terminal:


Let’s open the file and create basic Django Models and print them on the screen.

save() method adds the model to the database.

Every time when you load this page new model would get appended. Hence at the beginning, we delete() all models.

For the choices, to display the right-hand side part we use the auto-generated method get_section_display()

Indentation matters!


Time to do the URL routing in the file.


Now run the following two commands:


CRUD Operations

Alternative way to create a new instance of Django Model:

Besides creating, deleting, retrieving which we have implemented till now, we can search the data as well using filters.

How to Filter greater than and less than?

Updating the Data

A metaclass is an inner class we can define inside Django Models. We can specify the verbose_name of the Django Model class and default ordering field name, db table name etc.

ordering = ["-size"] orders in descending order

For the complete list of features refer here

Foreign Keys

Let’s create a new Django Model and add foreign key

The str method defines a human-readable representation of the model that is displayed in the Django admin site and in the Django shell.

Foreign Key is linked to another class. This time a django inbuilt class.
on_delete field option tells what to do when the model is being deleted.

  • CASCADE: When the referenced object is deleted, also delete the objects that have references to it (When you remove a blog post, for instance, you might want to delete comments as well).
  • PROTECT: Forbid the deletion of the referenced object. To delete it you will have to delete all objects that reference it manually.
  • SET_NULL: Set the reference to NULL (requires the field to be nullable). For instance, when you delete a User, you might want to keep the comments he posted on blog posts, but say it was posted by an anonymous (or deleted) user.
  • SET_DEFAULT: Set the default value.


through is used to connect two tables through an intermediary class.
Let’s learn about this through an example use case.

Following is our class:

Save the file and let’s run the following command in the terminal:

This would open an interactive shell where we can create test data for the above Django Models.


We create sets of individual models and merge them in the classroom.
On querying the Teacher model, the students are automatically updated, thanks to through.

By admin

Leave a Reply

%d bloggers like this: