zoukankan      html  css  js  c++  java
  • 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)
    

      

  • 相关阅读:
    做代码的曲线问题
    全书目录
    关于《Delphi源代码分析》的讨论
    说说“从编程到工程”专栏的由来
    CF285E Positions in Permutations
    CF264B Good Sequences
    CF115E Linear Kingdom Races
    CF633F The Chocolate Spree
    CF767C Garland
    CF559C Gerald and Giant Ches
  • 原文地址:https://www.cnblogs.com/yehewudi/p/9565562.html
Copyright © 2011-2022 走看看