Creating Models in Django
Django is a Model-View-Template (MVT) Structure Python framework, by convention when creating an application with Django, it starts with creating the Model, View then Template. MVT is a software design layout for web application development. In this article I will be walking you through:
Model: What does it mean?
How to create a model using Django
What is a Model?
Model is like built-in features that Django uses to create tables in their field and several restrictions.
The model is responsible for maintaining data. Models are like the interface of your data.
It is the practical data structure behind the whole application and it is represented by a database. Each model maps to a single database table. All fields created on the model.py file are going to reflect on the admin page.
So we can say Django models are like SQL but because SQL is more complicated and sometimes not easy to understand Django provides us with a Model which is imported from the Django database.
from django.db import model
How to create a Model
Creating a model in Django isn't so tasking. However, it's important to note the field type when creating a model in Django. For example, Charfield, TextField, ImageField, etc.
Note: When you want to create your models, you should already have the idea of the basic fields you need to create. in this case:
Other fields can be added as the project progresses.
Step two: Create your model following the code snippet below
#import model class from django database
from django.db import models
'''import user from django models
we want to connect to the superuser created in the admin
area, so we will be able to access it.'''
from django.contrib.auth.models import user
# Create your models here.
title = models.CharField(max_lenght=200)
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = models.TextField()
date_created = models.DateTimeField(auto_new_add=True)
return self.title + ' | ' + self.author #return self and also concatenate self.author
def str(self) is a function used to return the name of an object that is created with a specific method class.
return self.title + ' | ' + self.author. What this does is to allow us to see on the admin page the title of the article and the author's name instead of just numbers. Note: concatenating isn't compulsory.
It’s important to add str() methods to your models, not only for your own convenience when dealing with the interactive prompt, but also because objects’ representations are used throughout Django’s automatically-generated admin. ~ Django Documentation
Step three: Add the model to the admin. follow the code in the snippet below.
#import d-built-in admin
from django.contrib import admin
#import Article from models class
from .models import Article
# Register your models here.
Step four: make migrations
python manage.py makemigrations
Basically, makemigrations generate the SQL commends for preinstalled applications which you can see under installed applications in your root folder settings.py, and your newly created application's model which you have added to the installed apps. In other others we can say make migrations command is pushing the just created fields into the database.
Note: makemigrations is in plural.
Step five: Migrate file
python manage.py migrate
Migrate command executes those SQL commands in the database file.
Note: whenever you create a model, delete or edit anything on model.py of your project, you need to run the above command.
Step six: Run server
python manage.py runserver
This is done each time any changes are made on the project, so as to restart the server and to effect the changes made.
Yes! You have created your model. As you continue to build the project, there will be more fields to add to your model.py file just make sure at every you make those changes, you makemigrations, migrate, and then run server. You are good to go.
I really hope you find this article helpful. Thanks for reading. Feel free to hit the like button and drop your comment in the comment box below.