admin依赖
依赖APP:
django.contrib.admin
django.contrib.auth
django.contrib.contenttypesdjango.contrib.messagesdjango.contrib.sessionsINSTALLED_APPS
依赖模板的context_processors:
django.contrib.auth.context_processors.auth
django.contrib.messages.context_processors.messages
依赖的中间件:
django.contrib.auth.middleware.AuthenticationMiddleware
django.contrib.messages.middleware.MessageMiddleware
如何定义一个admin
@admin.register(Category) # 参数是你管理的哪一个表单
class CategoryAdmin(admin.ModelAdmin):
pass
基础的配置
1、list_display 用来配置列表页面展示哪些字段
list_display = ('title', 'category', 'status',
'created_time', 'owner', 'operator')
2、list_display_links 用来配置哪些字段可以作为链接,点击之后进入编辑页面
list_display_links = ('title', 'category')
3、list_filter 配置页面的过滤器,需要通过哪些字段来过滤列表页面
class CategoryOwnerFilter(admin.SimpleListFilter):
""" 自定义过滤器只显示当前用户分类 """
title = '分类过滤器'
parameter_name = 'owner_category'
def lookups(self, request, model_admin):
return Category.objects.filter(owner=request.user).values_list('id', 'name')
def queryset(self, request, queryset):
category_id = self.value()
if category_id:
return queryset.filter(category_id=self.value())
return queryset
@admin.register(Post, site=custom_site)
class PostAdmin