Session Overview

  • Category Model
  • Create Migration
  • Register Model in Admin
  • Adding Categories
  • Customize Django Admin Panel

Create Category Model

Categories help organize Blog Posts into different sections such as Technology, Sports, Education and Travel.

Each Blog Post will belong to one Category.

Open models.py
blog/models.py
Create Category Model
from django.db import models

class Category(models.Model):

    Id = models.AutoField(primary_key=True)

    Name = models.CharField(max_length=100)

    def __str__(self):

        return self.Name

    class Meta:

        db_table = "BlogCategories"
Explanation
  • Id - Unique identifier for each Category.
  • Name - Stores the Category Name.
  • __str__() - Displays the Category Name in the Django Admin Panel.
  • db_table - property allows us to specify a custom table name in the database.

Create Migration

After creating or modifying a Model, Django must update the database structure.

This is done using Migrations.

Step 1 - Generate Migration
python manage.py makemigrations

Django creates a migration file inside the migrations folder.

Step 2 - Apply Migration
python manage.py migrate

Django creates the blog_category table inside the MySQL database.

Verify Table

Refresh your MySQL database and verify that the table has been created successfully.

Migration Flow
Create Model

        ↓

makemigrations

        ↓

Migration File

        ↓

migrate

        ↓

Database Table
Remember

Whenever you create a new Model or modify an existing Model, execute the following commands:

python manage.py makemigrations

python manage.py migrate

Without these commands, the database will not reflect the changes made in your Model.

Register Model in Admin

By default, newly created Models do not appear in the Django Admin Panel.

We must register the Model before it can be managed through the Admin Panel.

Step 1 - Open admin.py
blog/admin.py
Step 2 - Register Category Model
from django.contrib import admin

from .models import Category

admin.site.register(Category)
Step 3 - Create Admin User
python manage.py createsuperuser

Enter the required details:

Username

Email Address

Password

Confirm Password
Step 4 - Run Project
python manage.py runserver
Step 5 - Open Admin Panel
http://127.0.0.1:8000/admin/
Result: After logging in, the Categories module will be displayed in the Django Admin Panel.
Why do we register the Model?

Registering a Model tells Django that it should be displayed in the Admin Panel.

Without registration, the Model exists in the database but cannot be managed through the Admin Interface.

Add Categories using Django Admin

The Django Admin Panel provides an easy way to insert, update and delete records without creating custom pages.

Step 1 - Login to Admin Panel
http://127.0.0.1:8000/admin/
Step 2 - Open Categories

Click on the Categories module.

Step 3 - Click Add Category

Enter a Category Name and click Save.

Example Categories
Technology

Education

Sports

Travel

Health

Business
Step 4 - Verify Database

Refresh the BlogCategories table in MySQL and verify that the records have been inserted successfully.

Result: The Category records are successfully stored in the MySQL database.

Customize Django Admin Panel

Django allows us to customize the Admin Panel to improve data management and branding.

In this section, we will customize the Category listing and change the Admin Panel title.

Step 1 - Customize Category Listing

Open blog/admin.py.

from django.contrib import admin

from .models import Category


@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):

    list_display = ['Id', 'Name']
Result: The Category list now displays both the Id and Name columns.
Step 2 - Add Search Box
search_fields = ['Name']
Result: A Search Box is displayed at the top of the Category page.
Step 3 - Add Default Sorting
ordering = ['Name']
Result: Categories are displayed in alphabetical order.
Step 4 - Change Admin Panel Title

Open blog/admin.py.

from django.contrib import admin

admin.site.site_header = "Blog CMS"

admin.site.site_title = "Blog CMS"

admin.site.index_title = "Welcome to Blog CMS"
Result: The default Django Administration title is replaced with our own Blog CMS branding.
Useful Admin Customizations
  • list_display - Display selected columns.
  • search_fields - Add a Search Box.
  • ordering - Sort records automatically.

Quick Revision

Category Model
  • models.Model
  • AutoField
  • CharField
Database
  • Meta Class
  • db_table
  • Migrations
Admin Panel
  • createsuperuser
  • Register Model
  • Manage Data
Admin Customization
  • list_display
  • search_fields
  • ordering
Admin Branding
  • site_header
  • site_title
  • index_title
Commands
  • makemigrations
  • migrate
  • createsuperuser
Commands Practiced Today
python manage.py makemigrations

python manage.py migrate

python manage.py createsuperuser

python manage.py runserver

Assignment

Continue Developing the News Portal

Implement the same functionality covered in today's Blog Project using your News Portal project.

  1. Create a NewsCategory model with the following fields:
    • Id
    • Name
  2. Use the Meta class to rename the database table to NewsCategories.
  3. Generate and apply migrations.
  4. Register the NewsCategory model in the Django Admin Panel.
  5. Create an Administrator account using createsuperuser.
  6. Add at least five News Categories through the Django Admin Panel.
  7. Customize the Admin Panel by:
    • Displaying Id and Name using list_display.
    • Adding a Search Box using search_fields.
    • Sorting records alphabetically using ordering.
    • Changing the Admin Panel title to News CMS.
  8. Verify that the NewsCategories table and all records are successfully created in the MySQL database.