Django.contrib
Django提供了基于web的管理工具
Django自动管理工具是Django.contrib的一部分,你可以在项目的settings.py中的INSTALLED_APPS看到它:
# Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app01.apps.App01Config', ]
Django.contrib是一套庞大的功能集,它是Django基本代码的组成部分
激活管理工具
通常我们在生成项目时会在url.py中自动设置好,当这些配置好后,Django管理工具就可以运行了
from django.conf.urls import url from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), ]
使用管理工具
启动开发服务器,然后在浏览器中访问http://127.0.0.1:8000/admin/,得到登录界面,可以通过命令python manage.py createsuperuser来创建超级用户。为了让admin界面管理某个数据模型,我们需要先注册该数据模型到admin
from django.db import models # Create your models here. class Author(models.Model): name=models.CharField( max_length=32) age=models.IntegerField() def __str__(self): return self.name class Publish(models.Model): name=models.CharField( max_length=32) email=models.EmailField() def __str__(self): return self.name class Book(models.Model): title = models.CharField( max_length=32) publishDate=models.DateField() price=models.DecimalField(max_digits=5,decimal_places=2) publisher=models.ForeignKey(to="Publish") authors=models.ManyToManyField(to='Author') def __str__(self): return self.title
admin.py
from django.contrib import admin from app01 import models # Register your models here. admin.site.register(models.Book) admin.site.register(models.Author) admin.site.register(models.Publish)
admin的定制
在admin.py中只需要将model中某个类注册,即可在Admin中实现增删改查的功能,但是这种方式比较简单,如果想要进行更多的定制操作,需要利用ModelAdmin
定制的两种方法
from django.contrib import admin from app01.models import * # 方式一 @admin.register(Book) class Bookconfig(admin.ModelAdmin): list_display = ['title','price'] # 方式二 class Bookconfig1(admin.ModelAdmin): list_display = ['title','price'] admin.site.register(Book,Bookconfig1)
定制功能
from django.contrib import admin from app01.models import * admin.site.register(Author) admin.site.register(Publish) @admin.register(Book) class Bookconfig(admin.ModelAdmin): # 定制显示的列 list_display = ['title','price'] # 定制列可以点击跳转 list_display_links = ['title'] # 定制右侧快速筛选 list_filter=['title'] # 列表查询是否自动select_related list_select_related = ['publisher'] # 列表显示时可以编辑的列 list_editable = ['price'] # 模糊搜索 search_fields = ['title'] # 对Date和DateTime类型进行搜索 date_hierarchy = 'publishDate' # 定制action中的操作 def patch_init(self,request,queryset): queryset.update(price=100) patch_init.short_description='批量初始化' actions=[patch_init] #详细页面,针对FK和M2M变成input框形式 raw_id_fields = ('publisher',) #详细页面,定制显示的字段 fields = ('title',) #详细页面,定制排除的字段 exclude = ('price',) #详细页面时,定制只读的字段 readonly_fields = ('title',) #详细页面时,使用fieldsets标签对数据进行分割显示 fieldsets = (('基本数据',{'fields':('title',)}),('其他',{'classes':('collapse',),'fields':('price',)}),) # 详细页面时,M2M显示时,数据移动选择 filter_vertical = ('authors',) #列表时,数据排序规则 ordering = ('-id',) # 详细页面时,使用radio显示选项(FK默认使用select) radio_fields = {'publisher':admin.VERTICAL} # 详细页面,如果有其他表和当前表做FK,那么详细页面可以进行动态增加和删除 class BookInline(admin.StackedInline): # TabularInline extra = 0 model = Book class GroupAdminMode(admin.ModelAdmin): list_display = ('id', 'title',) inlines = [BookInline, ]# empty_value_display = "列数据为空时,显示默认值" @admin.register(Book) class UserAdmin(admin.ModelAdmin): empty_value_display = "列数据为空时,默认显示" list_display = ('user', 'pwd', 'up') def up(self, obj): return obj.user up.empty_value_display = "指定列数据为空时,默认显示" # form = ModelForm,用于定制用户请求时候表单验证 from django.forms import ModelForm from django.forms import fields class MyForm(ModelForm): others = fields.CharField() class Meta: model = models = Book fields = "__all__" @admin.register(Book) class BookAdmin(admin.ModelAdmin): form = MyForm
定制HTML模板
#定制HTML模板 add_form_template = None change_form_template = None change_list_template = None delete_confirmation_template = None delete_selected_confirmation_template = None object_history_template = None