码迷,mamicode.com
首页 > 其他好文 > 详细

django administration 小记

时间:2018-08-31 14:47:31      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:pytho   模块   related   title   bsp   art   foreign   ted   search   

一、在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 小记

标签:pytho   模块   related   title   bsp   art   foreign   ted   search   

原文地址:https://www.cnblogs.com/yehewudi/p/9565562.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!