django administration 小记
Posted yehewudi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django administration 小记相关的知识,希望对你有一定的参考价值。
一、在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)
以上是关于django administration 小记的主要内容,如果未能解决你的问题,请参考以下文章