一、在modlues模块中添加如下代码
from django.db import models from django.utils import timezone from django.contrib.auth.models import User # Create your models here. class BlogArticles(models.Model): title = models.CharField(max_length=300) author = models.ForeignKey(User, related_name="blog_posts") body = models.TextField() publish = models.DateTimeField(default=timezone.now) class Meta: ordering = ("-publish",) def __str__(self): return self.title
使用命令python manage.py makemigrations
python manage.py migrate3
python manage.py createsuperuser # 创建超级管理员用户
二、在admin模块中添加如下代码
from django.contrib import admin from .models import BlogArticles # Register your models here. class BlogArticlesAdmin(admin.ModelAdmin): list_display = ("title", "author", "publish") # 在描述中显示对应的数据库字段 list_filter = ("publish", "author",) # 在右方添加标签页,可以已数据库字段分类 search_fields = ('title', "body") # 搜索功能()中添加要搜索的字段 raw_id_fields = ("author",) date_hierarchy = "publish" ordering= ['publish', 'author'] # 给list_filter添加排序功能 admin.site.register(BlogArticles, BlogArticlesAdmin)