The Django web framework has a model-view-model (MVT) architecture, making it the only framework you’ll need to build a complete website or web application. This Python framework lets you create templates that generate databases and render dynamic HTML templates to the UI using views.
Django’s power is no secret; it’s fast, reliable, scalable and secure. The reliability, as well as the scalability of this software, is based on its MVT architecture. And in this article, you’ll learn exactly how Django’s MVT architecture works.
What is the Django model?
Django’s MVT architecture model defines the structure and behavior of the data you want to store through your website. Each Django model you create generates a corresponding database table, where each attribute in the model becomes a field in the table.
Continuing the setup from our Django introductory article, you can create a template for vendors. A seller can have a seller template containing personal information, such as name and contact information, and an associated template for the items each seller sells.
The example existing Django project file structure
Creating Django models
If you look under the sellers application section in the file structure above, you will see a file called models.py. This is where you will create all your Django templates for the sellers section of your website. Each model you create will be a subclass of Django Model APIthat’s why every product generated by Django models.py the file has a default templates import.
The models.py file
From django.db import models
# Create your models here.
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
contact_number = models.CharField(max_length=30)
seller = models.ForeignKey(Seller, on_delete=models.CASCADE)
item_name = models.CharField(max_length=100)
item_qantity = models.IntegerField()
item_price = models.DecimalField(max_digits=9, decimal_places=2)
item_description = models.TextField()
The code above is a copy of the updated content of the model.py file. The file now creates two models: Seller and Product. These models share a one-to-many relationship, where a seller can have multiple products for sale. Thus, the product model has a foreign key of the seller and a on_delete attribute set to models.CASCADEwhich means that when you delete a seller, you automatically delete all products that have it primary key like a foreign key.
You might also notice that each of the patterns in the code above does not have a primary key. This is because Django will automatically generate a primary key if you don’t explicitly create one.
Before you can use a model you create, you need to tell Django where to find it. To do this, you will need to navigate to the settings.py file and insert the name of the module that contains the models.py file, in the INSTALLED_APP section.
In the example project of this article, the models.py the file is in the sellers module. Therefore, the update INSTALLED_APP section will read as follows:
INSTALLED_APPS = [
With the code above, the vendor app templates are now visible on the Django website, and you can now move on to migrations.
Performing migrations is important because this process is a way to release the changes you make in your models to the corresponding database schema. So, whenever you make changes to your model, you will need to go through the migration process, which involves two steps.
The first step is to perform migrations, which is a command that creates migrations based on the changes detected in the models.py file. To start the migration process, you will need to open your terminal, navigate to the directory containing your Django project, and launch the server using the following command:
python manage.py runserver
With the server running in a terminal, open a new terminal and enter the following command:
python manage.py makemigrations
After execution, the terminal will produce the following output:
Migrations for 'sellers':