Django Models
In previous page we learn overview of MVT architecture with some overview of Models, View and Templates. Now we learn complete Django Models with project code.
A Model in Django is a class imported from the django library that acts as the bridge between your database and server.
When you create a model, Django executes Query to design a corresponding table in the database.
This file is only importing the models file of the django library:
Models.py
>from django import models
Creating Django Model
First create an app using previous command and then open models.py file:
from django.db import models
class MyModelName(models.Model):
# Fields
field_name = models.CharField(max_length=20, help_text='Enter field documentation')
...
# Metadata
class Meta:
ordering = ['-field_name']
In this we can add more fields add these fields in meta class.
Django Migrations:
And then run these two commands
>python manage.py makemigrations
>python manage.py migrate
Django Superuser:
And then create superuser by using these commad and check table by admin
>python manage.py createsuperuser
Models are basically used to create table in database table, so after this your database table is created and your can adit after go through admin panel which is created after superuser command.