Skip to content

Latest commit

 

History

History
134 lines (114 loc) · 4.54 KB

admin.md

File metadata and controls

134 lines (114 loc) · 4.54 KB

admin界面设置

Admin Site 管理界面

官网

最简单的

from django.contrib import admin
admin.site.register(models)
@admin.register(models.Model)
class MyModelAdmin(admin.ModelAdmin):
    ...
admin.site.site_header = 'Django administration'
  • site_header: 修改全局的标题,默认 Django administration
  • site_title: 页面的title
  • list_display: 在列表页面需要显示的字段数据
  • list_filter: 在列表页面,可以进行分类查看的数据
  • list_per_page: 默认100, 一页多少数据
  • list_select_related: 哪些字段要通过join先查找出来,避免太多sql语句
  • search_fields: 在列表页面的搜索框搜索的字段
  • readonly_fields: 在详情页面,哪些字段只能读,不能写。一般用于auto_now_add 和 auto_now 的时间,以及id

看到stackoverflow上的问题,如何自定义一个字段,创建的时候有,编辑的时候没有

def get_readonly_fields(self, request, obj=None):
    if obj is None:
        return []
    else: return self.readonly_fields
class FlatPageAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {
            'fields': ('url', 'title', 'content', 'sites')
        }),
        ('Advanced options', {
            'classes': ('collapse', 'wide', 'extrapretty'),  # collapse会让几个字段默认隐藏
            'fields': ('registration_required', 'template_name'),
            'description': '描述',
        }),
    )
  • ...
  • radio_fields
  • autocomplete_fields 实现在添加Choice的时候,可以搜索question的question_text字段
    为了防止数据的泄露。用户创建时,必须要有外键model的view和change权限
class QuestionAdmin(admin.ModelAdmin):
    ordering = ['date_created']
    search_fields = ['question_text']  # 必须能search才能autocomplete
class ChoiceAdmin(admin.ModelAdmin):
    autocomplete_fields = ['question']
  • raw_id_fields
  • ...
from django.contrib import admin
class BookInline(admin.TabularInline):
    model = Book
class AuthorAdmin(admin.ModelAdmin):
    inlines = [
        BookInline,
    ]

[自定义]

    from django.utils.html import format_html

    class PersonAdmin(admin.ModelAdmin):
        list_display = ('name', 'thumbnail')

        def thumbnail(self, obj):
            return format_html('<img src="%s">' % obj.avatar)
  • has_add_permission(request): 能否添加
  • has_delete_permission(request, obj=None): 能否删除

设置样式

    class SampleAdmin(admin.ModelAdmin):
        fieldsets = [
            (None, {'fields': ['text']})
        ]
        list_display = ('text','project')
        list_filter = ['time','leve']
        search_fields = ['text','project', 'get_users']
        def get_users(self, obj):  # 自定义一个字段
            return obj.users.all()
        get_users.short_description = '用户'
    admin.site.register(<models>, SampleAdmin)

Admin actions

from django.contrib import admin
from myapp.models import Article


@admin.action(description="Mark selected stories as published")
def make_published(modeladmin, request, queryset):
    queryset.update(status="p")


class ArticleAdmin(admin.ModelAdmin):
    list_display = ["title", "status"]
    ordering = ["title"]
    actions = [make_published]


admin.site.register(Article, ArticleAdmin)

Admin documentation generator