A Model is a Python class used to represent a database table.
Django automatically creates database tables based on Model definitions.
| Model | Database Table |
|---|---|
| Category | Category Table |
| Post | Post Table |
| Comment | Comment Table |
Model
↓
Migration
↓
Database Table
↓
Store Data
We will create a Category model for our Blog Project.
demo/models.py
from django.db import models
class Category(models.Model):
Id = models.AutoField(primary_key=True)
Name = models.CharField(max_length=100)
| Code | Purpose |
|---|---|
| models.Model | Base class for all Models |
| Category | Model Name |
| CharField | Stores Text Data |
| max_length=100 | Maximum 100 Characters |
| AutoField | Auto Increment Primary Key |
The Id field is the Primary Key of the Category table. AutoField automatically generates sequential values such as: 1, 2, 3, 4, ...
Migrations are used to create or update database tables based on Model changes.
Whenever a Model is added or modified, migrations must be executed.
python manage.py makemigrations
Migrations for 'demo':
demo\migrations\0001_initial.py
+ Create model Category
python manage.py migrate
Applying demo.0001_initial... OK
These tables are required for Login, Admin Panel, Permissions and Session Management.
Django automatically creates table names using:
AppName_ModelName
App Name : demo
Model Name : Category
Table Name : demo_category
We can specify our own table name using Meta class.
from django.db import models
class Category(models.Model):
Id = models.AutoField(primary_key=True)
Name = models.CharField(max_length=100)
class Meta:
db_table = 'Category'
Category
Django will create the table with the exact name specified in db_table.
Create Model
↓
makemigrations
↓
migrate
↓
Database Table Created
Missing any step will prevent the table from being created.
Django uses SQLite by default and stores data in:
db.sqlite3
To view tables and data inside VS Code, install:
SQLite Viewer
After installation, open db.sqlite3 and browse tables directly from VS Code.
Django provides a built-in Administration Panel for managing application data.
Administrators can add, edit and delete records without writing additional code.
python manage.py createsuperuser
Username : admin
Email : admin@gmail.com
Password : ********
python manage.py runserver
http://127.0.0.1:8000/admin/
Creating a Model and running migrations creates the database table.
However, the Model will not appear in the Django Admin Panel until it is registered.
demo/admin.py
from django.contrib import admin
from .models import Category
admin.site.register(Category)
python manage.py runserver
http://127.0.0.1:8000/admin/
After registering the Model, records can be inserted directly from the Admin Panel.
http://127.0.0.1:8000/admin/
Click on Category and then click the Add button.
| Id | Name |
|---|---|
| 1 | Technology |
| 2 | Travel |
| 3 | Food |
The records entered through the Admin Panel are stored in the Category table.
By default, Django displays:
Category object (1)
To display the Category Name instead:
from django.db import models
class Category(models.Model):
Id = models.AutoField(primary_key=True)
Name = models.CharField(max_length=100)
class Meta:
db_table = 'Category'
def __str__(self):
return self.Name
Now the Admin Panel will display the actual Category Name.
The __str__() method specifies what should be displayed when an object is converted to text.
Without __str__(), Django Admin displays:
Category object (1)
Category object (2)
After adding:
def __str__(self):
return self.Name
Django Admin displays:
Technology
Travel
Food
In simple words, __str__() tells Django which field should be shown as the display name of the object.
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
Create Model
↓
makemigrations
↓
migrate
↓
Register Model
↓
Admin Panel
↓
Add Records
Id = models.AutoField(primary_key=True)
Name = models.CharField(max_length=100)
class Meta:
db_table = 'Tag'
def __str__(self):
return self.Name