第一步:编辑应用的admin.py 文件,编辑内容如下:
from django.contrib import admin
from blog.models import * # 导入所有模型类
@admin.register(Article) #Article 模型注册至后台
class ArticleAdmin(admin.ModelAdmin):
list_display = ('title', 'category', 'pub_time') # Article 数据模型显示字段
admin.site.register((Category, Comment, Tag)) # 多个模块注册到后台
第二步:编辑应用的models.py文件,编辑内容如下:
from django.db import models
from django.contrib.auth.models import User # 使用Django自带的用户模型
from ckeditor.fields import RichTextField
class Category(models.Model): # 文章类别
id = models.IntegerField(primary_key=True)
name = models.CharField('类别', max_length=20, unique=True)
class Meta:
verbose_name_plural = verbose_name = '类别'
def __str__(self):
return self.name
class Tag(models.Model): # 文章标签
name = models.CharField('标签', max_length=20, unique=True)
class Meta:
verbose_name_plural = verbose_name = '标签'
def __str__(self):
return self.name
# 文章类
class Article(models.Model): # 文章
id = models.AutoField(primary_key=True)
author = models.ForeignKey(User, on_delete=models.DO_NOTHING, verbose_name='作者')
title = models.CharField('标题', max_length=50)
content = RichTextField('内容',config_name='mycfg')
pub_time = models.DateField('日期', auto_now=True)
category = models.ForeignKey(Category, on_delete=models.SET_DEFAULT, default=1, verbose_name='类别')
tag = models.ManyToManyField(Tag, verbose_name='标签')
class Meta:
verbose_name_plural = verbose_name = '文章'
def __str__(self):
return self.title
class Comment(models.Model): #评论
id = models.AutoField(primary_key=True)
name = models.CharField('昵称', max_length=20)
email = models.EmailField('邮箱')
content = models.TextField('内容')
publish = models.DateField('时间', auto_now=True)
article = models.ForeignKey(Article, on_delete=models.CASCADE, verbose_name='文章')
reply = models.ForeignKey('self', on_delete=models.DO_NOTHING, null=True, blank=True, verbose_name='回复')
class Meta:
verbose_name_plural = verbose_name = '评论'
def __str__(self):
return self.content
第三步:后台效果展示: