decorators.admin 模块

模块: borax.decorators.admin

attr

函数签名

  1. attr(**kwargs)

设置函数对象的属性。

display_field

函数签名

  1. display_field(short_description, admin_order_field=None, **kwargs)

使用装饰器定义回调函数的 list_display

原始例子:

  1. def upper_case_name(obj):
  2. return ("%s %s" % (obj.first_name, obj.last_name)).upper()
  3. upper_case_name.short_description = 'Name'
  4. class PersonAdmin(admin.ModelAdmin):
  5. list_display = (upper_case_name,)

使用 @display_field 改写如下:

  1. @display_field(short_description='Name')
  2. def upper_case_name(obj):
  3. return ("%s %s" % (obj.first_name, obj.last_name)).upper()
  4. class PersonAdmin(admin.ModelAdmin):
  5. list_display = (upper_case_name,)

action

函数签名

  1. action(short_description=None, allowed_permissions=None, **kwargs)

使用装饰器定义 action 函数 。

例子:

  1. def make_published(modeladmin, request, queryset):
  2. queryset.update(status='p')
  3. make_published.short_description = "Mark selected stories as published"

改写后:

  1. @action(short_description="Mark selected stories as published")
  2. def make_published(modeladmin, request, queryset):
  3. queryset.update(status='p')