Django admin
Django admin使用
官方文档: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#
一、list_display = ('title', 'pub_date', 'update_time',)
ModelAdmin.list_display
class Person(models.Model): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) # 自定义方法 def my_property(self): return self.first_name + ' ' + self.last_name my_property.short_description = "Full name of the person" full_name = property(my_property)
在admin.py中,list_display 就是来配置要显示的字段的,当然也可以显示非字段内容,或者字段相关的内容
from django.contrib import admin from .models import Article, Person class ArticleAdmin(admin.ModelAdmin): # 在django中横像显示的方法 list_display = ('title', 'pub_date', 'update_time',) class PersonAdmin(admin.ModelAdmin): list_display = ('full_name',) # 绑定modules中的class admin.site.register(Article, ArticleAdmin) admin.site.register(Person, PersonAdmin)
二、搜索功能:search_fields = ('title', 'content',) 这样就可以按照 标题或内容搜索了
ModelAdmin.search_fields
CharField(普通字段) search_fields = ('title', 'content',) ForeignKey(外键) search_fields = ['user__email']
三、筛选功能:list_filter = ('status',) 这样就可以根据文章的状态去筛选,比如找出是草稿的文章
ModelAdmin.
list_filter
class PersonAdmin(admin.ModelAdmin): list_filter = ('is_staff', 'company') class PersonAdmin(admin.UserAdmin): list_filter = ('company__name',) admin.site.redigest(Person, PersonAdmin)
四、新增或修改时的布局顺
from django.contrib import admin class FlatPageAdmin(admin.ModelAdmin): fieldsets = ( (None, { 'fields': ('url', 'title', 'content', 'sites') }), ('Advanced options', { 'classes': ('collapse',), 'fields': ('registration_required', 'template_name'), }), )