Django: Models

There are two popular types of software architectural patterns for developing web applications. There is the MVC(Model View Controller) and the MVT(Model View Template). The major difference is that in MVC the developer gets to code the controller that interacts with the view and the model while in MVT the framework handles the controller's activities such that the developer is left with a template.

Django follows the architectural pattern of MVC but takes it further in the sense that Django handles the controller part and in return there is a template. The template is a HTML file modified with DTL(Django Template Language). Hence why it is referred to as MVT. The Model is the data access layer that interacts with the database(more on this later). The view has functions that handle requests and return responses.

In Django, you create the view that requests from the model that you create and supplies to the template that you create. This is then mapped to a url and Django handles the rest.

kevin-ku-w7ZyuGYNpRQ-unsplash.jpg

MODELS

Unless you are building a static website(which is not common these days), you will need to have a database for storing and retrieving data. Django use SQLite by default but you are allowed to change it to any kind of database that you want.

In Django, models provide ways to interact directly with the database using ORM (Object Relational-Mapping). This simplifies the process of working with databases. In ORM what is created are classes of data and this means you don't have to have an in-dept knowledge about how SQL(Structured Query Language) works or work directly with it. Before we dive deep into models, lets first talk about the concept of the "Object" in ORM.

OBJECTS

I am sure we have heard about object-oriented programming. What it simply means is that instead of writing different codes that are random and not connected in anyway, a new way of programming was introduced. This new way of programming treats/model things as objects. For example animals are objects that have properties like color, no of legs, size, presence of scale, presence of fur etc and methods(functions) like MR NIGER (movement, respiration, nutrition, irritability, growth, excretion, reproduction etc). Now a class is what defines these properties and methods, it creates the object. This object can then be instantiated at different points in the program. For example, a cow can be made an instance of the class Animal.

Back to models, let me explain in context, if I were to build a forum where people post articles I can treat each post as an object created by a class that has properties such as author, article, title, date of post, time of post etc. The class post can also have a function like publish etc.

class Post(models.Model):

Then I will go ahead and define the properties

from django.db import models

class Post(models.Model):
          title = models.CharField(max_length = 100)
          article = models.TextField()
          author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
          date = models.DateTimeField(auto_now_add = True)

Then the method if any

class Post(models.Model):
         title = models.CharField(max_length = 100)
         article = models.TextField()
         author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
         date = models.DateTimeField(auto_now_add = True) 

         def __str__(self):
                   return self.title

When this model is created, it is mapped to a single table in the database. Django model simplifies the different queries related to CRUD(create, read, update and delete) data in SQL database.

After a model is successfully created in Django, you have to save the file then makemigrations and migrate it to ensure that it is updated in the database. You do that by opening your command line, change directory into your project root file and run the following commands

Python manage.py makemigrations
Python manage.py migrate

And you just created your first database and your first model in Django.

Cheers!

Cover Photo by Faisal M on Unsplash
Photo by Kevin Ku on Unsplash

Comments (2)

UDOKAKU UGOCHUKWU's photo

Nice