Admin管理工具
Django提供了基于web的管理工具
Django自动管理工具是django.contrib的一部分,可以在项目的settings.py的INSTALLED_APPS看到
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'TestModel',
]
激活管理工具
通常在生成项目时会在urls.py中自动设置好
url(r'^admin/',admin.site.urls),
# -*- coding:utf-8 -*-
from django.conf.urls import url
from django.contrib import admin
from django.http import HttpResponse
from . import view,testdb,search,search2
urlpatterns = [
url(r'^admin/',admin.site.urls),
url(r'^$',view.hello),
url(r'^testdb$',testdb.testdb),
url(r'^search-form$',search.search_form),
url(r'^search$',search.search),
url(r'^search-post$',search2.search_post),
]
使用管理工具
通过命令
python manage.py createsuperuser
来创建超级用户
输入账号密码登录
为了让admin界面管理某个数据模型,我们需要先注册该数据模型到admin
比如我们之前在TestModel中已创建了模型Test。修改TestModel/admin.py,刷新页面
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from TestModel.models import Test
# Register your models here.
admin.site.register(Test)
复杂模型
先在TestModel/models.py中增加一个更复杂的数据模型
下面代码里的两个表(Tag,Contact),Tag以Contact为外部键,一个Contact可以对应多个Tag
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.
#models.py
class Test(models.Model):
name = models.CharField(max_length=20)
class Contact(models.Model):
name = models.CharField(max_length = 200)
age = models.IntegerField(default = 0)
email= models.EmailField()
def __unicode__(self):
return self.name
class Tag(models.Model):
contact = models.ForeignKey(Contact)
name = models.CharField(max_length = 50)
def __unicode__(self):
return self.name
在TestModel/admin.py里注册多个模型并显示,多个模型用中括号括起来,逗号隔开
别忘了修改导入
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from TestModel.models import Test,Contact,Tag
# Register your models here.
admin.site.register([Test,Contact,Tag])
创建表结构
python manage.py makemigrations TestModel # 让 Django 知道我们在我们的模型有一些变更
python manage.py migrate TestModel # 创建表结构
数据库新增的表
自定义表单
如下,我们新增Contact时默认有三个字段,如果我想隐藏掉Age,可以这么做。
修改TestModel/admin.py
给Contact定义一个类,只展示name,email
然后注册时指定Contact使用这个类
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from TestModel.models import Test,Contact,Tag
# Register your models here.
class ContactAdmin(admin.ModelAdmin):
fields = ('name','email')
admin.site.register(Contact,ContactAdmin)
admin.site.register([Test,Tag])
也可以自定义格式,将输入栏分块,修改TestModel/admin.py
Main:主显示
Advance:可选显示
'classes':('collapse',),
是个css样式,折叠。
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from TestModel.models import Test,Contact,Tag
# Register your models here.
class ContactAdmin(admin.ModelAdmin):
fieldsets = (
['Main',{
'fields':('name','email'),
}
],
['Advance',{
'classes':('collapse',),#css
'fields':('age',),
}
]
)
admin.site.register(Contact,ContactAdmin)
admin.site.register([Test,Tag])
内联显示
上面的Contact是Tag的外部键,可以使用内联显示,将Tag附加在Contact的编辑页面显示
修改TestModel/admin.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from TestModel.models import Test,Contact,Tag
# Register your models here.
class TagInline(admin.TabularInline):
model = Tag
class ContactAdmin(admin.ModelAdmin):
inlines = [TagInline] #Inline
fieldsets = (
['Main',{
'fields':('name','email'),
}
],
['Advance',{
'classes':('collapse',),#css
'fields':('age',),
}
]
)
admin.site.register(Contact,ContactAdmin)
admin.site.register([Test,Tag])
列表页显示
自定义列表页面展示,在ContactAdmin中增加list_display属性
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from TestModel.models import Test,Contact,Tag
# Register your models here.
class TagInline(admin.TabularInline):
model = Tag
class ContactAdmin(admin.ModelAdmin):
list_display = ('name','age','email') #list
inlines = [TagInline] #Inline
fieldsets = (
['Main',{
'fields':('name','email'),
}
],
['Advance',{
'classes':('collapse',),#css
'fields':('age',),
}
]
)
admin.site.register(Contact,ContactAdmin)
admin.site.register([Test,Tag])
在列表增加搜索栏
修改TestModel/admin.py,使用search_fields
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from TestModel.models import Test,Contact,Tag
# Register your models here.
class TagInline(admin.TabularInline):
model = Tag
class ContactAdmin(admin.ModelAdmin):
list_display = ('name','age','email') #list
search_fields = ('name',) #search
inlines = [TagInline] #Inline
fieldsets = (
['Main',{
'fields':('name','email'),
}
],
['Advance',{
'classes':('collapse',),#css
'fields':('age',),
}
]
)
admin.site.register(Contact,ContactAdmin)
admin.site.register([Test,Tag])