Django admin进阶

时间:2021-09-27 05:02:19

1.

ModelAdmin.inlines

将有外键的子类包含进视图 ,实例:

class Author(models.Model):
name = models.CharField(max_length=100) class Book(models.Model):
author = models.ForeignKey(Author)
title = models.CharField(max_length=100)
class BookInline(admin.TabularInline):
model = Book class AuthorAdmin(admin.ModelAdmin):
inlines = [
BookInline,
]

2.

list_display = ('first_name', 'last_name')

列表显示字段。记住:他不会显示manytomany字段

特殊用法:

class Person(models.Model):
first_name = models.CharField(max_length=50)
birthday = models.DateField() def born_in_fifties(self):
return self.birthday.strftime('%Y')[:3] == '195'
born_in_fifties.boolean = True class PersonAdmin(admin.ModelAdmin):
list_display = ('name', 'born_in_fifties')

将生日为195的设置为true,并且显示对号或者横线。 用途,学生成绩是否及格,当大于60设置为true。

list_display = ('__unicode__', 'some_other_field')
colored_first_name.admin_order_field = 'first_name'

设置字段排序

list_display_links = ('first_name', 'last_name')

设置超链接字段

3.ModelAdmin.list_editable

设置列表字段是否可以编辑,常用语文章更改目录,修改某些非外键字段。不过外键也可以修改,如果某个字段设置为links,则该字段不能被修改

#    raw_id_fields = ('goods',)
#    list_editable=('goods','price','num','producer','in_times','is_active')

4.

list_filter = ('is_staff', 'company')

过滤字段。

自定义过滤字段:

from datetime import date

from django.utils.translation import ugettext_lazy as _
from django.contrib.admin import SimpleListFilter class DecadeBornListFilter(SimpleListFilter):
title = _('decade born')#过滤标题 # 设置url参数,?decade='80s'
parameter_name = 'decade' def lookups(self, request, model_admin):
"""
值,显示属性
"""
return (
('80s', _('in the eighties')),
('90s', _('in the nineties')),
) def queryset(self, request, queryset):
"""
过滤
"""
if self.value() == '80s':
return queryset.filter(birthday__gte=date(1980, 1, 1),
birthday__lte=date(1989, 12, 31))
if self.value() == '90s':
return queryset.filter(birthday__gte=date(1990, 1, 1),
birthday__lte=date(1999, 12, 31)) class PersonAdmin(ModelAdmin):
list_filter = (DecadeBornListFilter,)
from django.contrib.admin import BooleanFieldListFilter

class PersonAdmin(ModelAdmin):
list_filter = (
('is_staff', BooleanFieldListFilter),
)

boolean类型的filter

class FilterWithCustomTemplate(SimpleListFilter):
template = "custom_template.html"

自定义过滤器模板默认模板 admin/filter.html

5.

list_max_show_all

显示全部 最大数据,总数据超过该数据将不显示显示全部

list_per_page

没页显示数据条数

ordering

排序

class ArticleAdmin(admin.ModelAdmin):
prepopulated_fields = {"字段1": ("ziduan2",)}

当字段1填写数据的同时,字段2也同时被填充,数据一致

class PersonAdmin(admin.ModelAdmin):
radio_fields = {"group": admin.VERTICAL}

单选按钮,适用于开关(必须填)。如是否有效

class ArticleAdmin(admin.ModelAdmin):
raw_id_fields = ("newspaper",)

外键选择

from django.utils.html import format_html_join
from django.utils.safestring import mark_safe class PersonAdmin(ModelAdmin):
readonly_fields = ('address_report',) def address_report(self, instance):
# assuming get_full_address() returns a list of strings
# for each line of the address and you want to separate each
# line by a linebreak
return format_html_join(
mark_safe('<br/>'),
'{0}',
((line,) for line in instance.get_full_address()),
) or "<span class='errors'>I can't determine this address.</span>" # short_description functions like a model field's verbose_name
address_report.short_description = "Address"
# in this example, we have used HTML tags in the output
address_report.allow_tags = True

只读,注意:可以向模板页面一样格式化数据,同时给只读属性增加自定义样式。

6.

6.ModelAdmin.save_as

当是修改数据的时候,将新增一个替换为保存为新的,并且旧的不替换,新增id保存

7.

ModelAdmin.save_on_top

设置保存按钮到顶部!

8.

search_fields = ['foreign_key__related_fieldname']

设置搜索。