Complete Django Models | Django Tutorials

Django Models
Django Django Models Django Tutorials

Complete Django Models | Django Tutorials

Complete Django Models

Model is the source of information about a single entity. When you create a model for your entity then you have to define different fields about entity and behavior related to that entity. When we create an application in your Django project, then you will get different files by default, in that files, there is a models.py file also exist. In the models.py file, you have to create different kinds of models you want to create for your project. It is very easy to create Django models to store data for your project’s related.
When we create a Django website then we have to render data dynamically, retrieving it from the database. In the database, how data will be store? Using Models. So you have to create models classes that will be your database tables in which relevant data will be store.

Why we used Django Models

You know, every application needs database, in the database we store data related to applications, users, etc. When a user visits an application and submits any query, or creates accounts, gives comments, etc all this information is stored in the database, for this reason, we have to create a different type of model in which our different data will be store. Now a day every application needs a database, without a database your applicants did not run properly.

Another reason is that we want to show dynamically data to the user, because now a day, there are 1% websites are static one, all other 99% are dynamic website. Dynamic websites are those which change data dynamically. We cannot change data dynamically without creating a database. So we need the Django models to get such functionality.

Django Models Example

from django.db import models

class Student(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30) 
    city = models.CharField(max_length=30)

Run the following command to create a SQL coding:

python manage.py makemigrations

Mapping Django models with SQL query

If you noticed, the following lines of code are in SQL. Because when we create a model class in defining some fields, after doing this we run a makemigrations command, when we run this command, it converts it into SQL coding. But your SQL code did not run, it will execute and your table will create when we run another command. These both commands we will learn in detail in this tutorial.

CREATE TABLE appname_student ( 
    “id” serial NOT NULL PRIMARY KEY, 
    “first_name” varchar(30) NOT NULL,
    “last_name” varchar(30) NOT NULL,
    “city” varchar(30) NOT NULL
     );

Run the following command to create a table according to the above SQL command:

python manage.py migrate

All Django Models Fields

There are different types of Django model fields, you will have to use while creating a model class for your project. When you develop a project, there will be a need for some tables in which the project’s related information will be store. You can also read our article on Django Form fields, in which you will get more knowledge.

These are the following model fields which are used for different purposes. You can use any of them. We collected Django’s official documentation.

  • AutoField
  • BigAutoField
  • BigIntegerField
  • BinaryField
  • BooleanField
  • CharField
  • DateField
  • DateTimeField
  • DecimalField
  • DurationField
  • EmailField
  • FileField
  • FilePathField
  • FloatField
  • ForeignKey
  • ImageField
  • IntegerField
  • NullBooleanField
  • PositiveBigIntegerField
  • IPAddressField
  • GenericIPAddressField
  • JSONField
  • PositiveIntegerField
  • PositiveSmallIntegerField
  • SlugField
  • SmallAutoField
  • SmallIntegerField
  • ManyToManyField
  • TextField
  • TimeField
  • URLField
  • UUIDField

Automatic primary key fields

The primary key is the key or attribute which uniquely identifies a row or record in a database table. Every table should have a specific primary key. Without a primary key, we did not identify a specific record. For this reason, we have to select any primary key which has a unique value. So Django by default creates a primary key for every model as we create. So you did not need to create a primary key separately.

Creating Models

To create a Django model, you have to define a model class. The class name should be meaningful and it will be your database entity. All the fields will be related to the entity. As in this case, we created a class with Studnet’s name, all the information should be related to the Student as its name, city, address, etc.

class Student(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    city= models.CharField(max_length=100)

When you run a command after the above coding the Django will convert the above model class coding into SQL coding as below:

python manage.py makemigrations

This command will generate a migration file in which following code you will find.

CREATE TABLE appname_student (
“id” serial NOT NULL PRIMARY KEY,
“first_name” varchar(50) NOT NULL,
“last_name” varchar(50) NOT NULL,

“city” varchar(100) NOT NULL,
);

After running python manage.py makemigrations command you have to run another command as following.

python manage.py migrate

As you run the above command, the SQL query as generated with 1st command, 2nd command will perform an action on the migration file as created through python manage.py makemigrations command. Then a database table will be created in your project’s database with the student name.

Conclusion

We have discussed different aspects related to Django Models. As you have understood what is Model in Django, the very important thing you have to notice is that, create as different models classes for your project as possible. But you should know, each entity has a separate model class. Because you have to work on the Normalization of database also. If you don’t know what is Normalization then you can read the article on it.

What is Normalization in Database?

If you have any questions in your mind while reading out the tutorial, then must share them with us. We will provide you the best solution possible.

If you use Facebook, Twitter, Pinterest, LinkedIn, or other social media platforms, then share this Django tutorial, if you share, more students will take the chance to learn.

Recommended Django Tutorials for you

Complete Tutorials on Django ModelForm

Complete Guide on Django ORM | Django Tutorials

Full Explanation of Django Form | Django Tutorials 

Complete Tutorial on Django Form Validation | Django Tutorials

How to Retrieve DjangoForm Data | Django Tutorials

Leave your thought here

Your email address will not be published. Required fields are marked *

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar
Compare

Get your Enrollment

50%OFF

Leave your details below and receive a discount coupon in your inbox