关于admin 的资源在官网上有:
writing your first Django app part 7
主要内容有:
Customize the admin form
自定义表单的域
#polls/admin.py
from django.contrib import admin
from .models import Question
class QuestionAdmin(admin.ModelAdmin):
fields = ['pub_date', 'question_text']
admin.site.register(Question, QuestionAdmin)
划分区域
#polls/admin.py
from django.contrib import admin
from .models import Question
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_text']}),
('Date information', {'fields': ['pub_date']}),
]
admin.site.register(Question, QuestionAdmin)
Adding related objects
非常重要的部分。需要强调的是,当前的表必须是别的表的外键
polls/admin.py
from django.contrib import admin
from .models import Choice, Question
class ChoiceInline(admin.StackedInline):
model = Choice
extra = 3
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_text']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
inlines = [ChoiceInline]
admin.site.register(Question, QuestionAdmin)
或者这样定义:
ChoiceInline
class ChoiceInline(admin.TabularInline):
#...
Customize the admin change list
自定义change list 显式的内容
class QuestionAdmin(admin.ModelAdmin):
# ...
list_display = ('question_text', 'pub_date', 'was_published_recently')
question_text,pub_date 是Question的字段
而was_published_recently 是Question方法,充分证明了python中的一切都是对象。。
增加过滤项
list_filter = ['pub_date']
增加搜索项
search_fields = ['question_text']
改变admin的样式
admin虽然强大,但是总得改改样子吧。。
不然三天两头只能看见那个鬼样子。
具体的步骤是这样子的,如果admin这个app需要寻找templates的话,它首先会在你规定的templates中寻找,所以我们不需要去修改它的源码。只需要将
djaong/contrib/admin/templates/admin/base_site.html
中的文件拷贝到工程的templates目录下,并且将这个templates添加到settings.py
中即可。
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
例如修改一下/admin/base_site.html这个文件:
{% extends "admin/base.html" %}
{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}
{% block branding %}
<h1 id="site-name"><a href="{% url 'admin:index' %}">it all depends on you</a></h1>
{% endblock %}
{% block nav-global %}{% endblock %}
##Customizing your application’s templates¶
Astute readers will ask: But if DIRS was empty by default, how was Django finding the default admin templates? The answer is that, since APP_DIRS is set to True, Django automatically looks for a templates/ subdirectory within each application package, for use as a fallback (don’t forget that django.contrib.admin is an application).
Our poll application is not very complex and doesn’t need custom admin templates. But if it grew more sophisticated and required modification of Django’s standard admin templates for some of its functionality, it would be more sensible to modify the application’s templates, rather than those in the project. That way, you could include the polls application in any new project and be assured that it would find the custom templates it needed.