Introduction to Models
Let us try to imagine a scenario where we want to create a table in the database and store some information.
Now, to create a table and store data in the database we have two different approaches:
-
Write a raw SQL query and execute that query in the database server.
-
Using Django ORM
Django provides us with Object Relational Mapper which is also known as ORM in short form and a Model is a part of Django ORM.
-
Using those models, we will write some python code and Django ORM will create some migration files based on our python code.
-
These migration file contains all the changes like creating a table, deleting a table, creating a column in the table, updating them, and much more such pieces of information.
-
When we execute this migration file on the database server, whatever changes are stored in the migration file, it gets executed on the database.
So, this is a short overview of models in Django.
Now, we have understood what is a model and why we need it. So, let us discuss how to create the model in Django.
-
Each model is a Python class that subclasses django.db.models.Model. So, to write a model, you will create a python class and inherit the Model from django.db.models.
-
Each attribute of the model represents a database field. That means whatever attribute you will create in that python class, it will be treated as a table column.
Now, you have learned how to create a model in Django, let us discuss some terminologies which are required to create a database field.
The first terminology is Model Field Type.
-
Consider a scenario where you want to store integer data in one column and character data in another column.
-
If you want to create such type of table, then while writing a raw SQL query, you will always need to specify the data type of the column.
-
In the case of Django ORM, whenever you create a database field, you need to specify what type of data you want to store.
-
This is where the model field type comes into the picture. Using the model field type, we can specify the data type of the database field.
-
So, if you want to store character data, you will use models.CharField, if you want to store decimal data, you will use models.DecimalField and in django documentation, you will find the list of model field types that you can use in your project.
After learning what model field type is, let us discuss about another terminology i.e. Model Field Option.
-
Each model field takes an optional argument which is known as model field options.
-
Using model field options we can impose certain conditions over the model field.
-
For example, if you want to store only 10 characters in any database field then you will use max_length = 10 as the model field option.
-
If you want some model field to remain null then you can set null = True.
-
If you want to provide some choices to select, then you have to use choices model field options.
Therefore, using the model field option you can apply extra functionalities over your model field.