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 .
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
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)
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)
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
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
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.