The Post Model stores all Blog Articles published on the website.
Each Post belongs to one Category and contains the Title, Summary, Image, Description and Publishing Information.
from ckeditor.fields import RichTextField
class Post(models.Model):
Id = models.AutoField(primary_key=True)
Category = models.ForeignKey(
Category,
on_delete=models.CASCADE
)
Title = models.CharField(max_length=200)
Slug = models.SlugField(unique=True)
Summary = models.CharField(max_length=300)
Image = models.ImageField(upload_to='uploads/')
Description = RichTextField()
PublishDate = models.DateField()
IsPublished = models.BooleanField(default=True)
def __str__(self):
return self.Title
class Meta:
db_table = "BlogPosts"
| Field | Purpose |
|---|---|
| Category | Links the Post to a Category. |
| Title | Stores the Blog Title. |
| Slug | Creates SEO-friendly URLs. |
| Summary | Short description displayed on the Home Page. |
| Image | Stores the Featured Image. |
| Description | Stores the complete Blog Content. |
| PublishDate | Date on which the article is published. |
| IsPublished | Controls whether the Blog is visible on the website. |
The RichTextField provided by CKEditor allows users to write formatted content instead of plain text.
It supports features such as:
The Post Model combines multiple Django Field Types to represent a complete Blog Article.
BlogCategories
Id Name
1 Technology
2 Sports
3 Travel
│
│ Foreign Key
▼
BlogPosts
Title : Django ORM
Category : Technology
Image : django.jpg
Summary : Learn Django ORM
Description : Rich Text Content
Publish Date : 01/07/2026
Published : Yes
Category = models.ForeignKey(
Category,
on_delete=models.CASCADE
)
| Field | Usage |
|---|---|
| Slug | Creates user-friendly URLs such as /post/django-orm. |
| Summary | Displayed on the Blog Listing page before opening the full article. |
| IsPublished | Allows administrators to hide or publish Blog Posts without deleting them. |
After creating the Post Model, we must update the database by generating and applying migrations.
python manage.py makemigrations
Django creates a new migration file inside the migrations folder.
python manage.py migrate
Django creates the BlogPosts table in the MySQL database.
Refresh your MySQL database and verify that the BlogPosts table has been created successfully.
Create Model
↓
makemigrations
↓
Migration File
↓
migrate
↓
BlogPosts Table
Register the Post Model to manage Blog Posts through the Django Admin Panel.
blog/admin.py
from django.contrib import admin
from .models import Category, Post
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = [
'Id',
'Title',
'Category',
'PublishDate',
'IsPublished'
]
search_fields = [
'Title',
'Summary'
]
list_filter = [
'Category',
'IsPublished',
'PublishDate'
]
ordering = [
'-PublishDate'
]
CKEditor is a Rich Text Editor that allows users to create formatted Blog content without writing HTML code.
It provides features such as headings, bold text, lists, tables and hyperlinks.
pip install django-ckeditor
Open blogproject/settings.py and add 'ckeditor' inside the INSTALLED_APPS list.
INSTALLED_APPS = [
...
'ckeditor',
]
from ckeditor.fields import RichTextField
Description = RichTextField()
Every Blog Post contains a Featured Image displayed on the Home Page and Blog Details page.
Django stores uploaded images inside the media folder.
pip install pillow
Image = models.ImageField(
upload_to='uploads/'
)
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
if settings.DEBUG:
urlpatterns += static(
settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT
)
media/uploads
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
Implement the same functionality covered in today's Blog Project using your News Portal.