I am trying to redefine my admin page for the auth.User
model. Everything is working properly, except for one thing. Check the code below:
我正在尝试重新定义auth.User模型的管理页面。除了一件事,一切都正常。检查以下代码:
from django.contrib import admin
from django.contrib.auth.models import User
from access.models import UserProfile
class UserProfileInline(admin.StackedInline):
model = UserProfile
class UserAdmim(admin.ModelAdmin):
inlines = [UserProfileInline,]
list_display = ['id', 'username', 'get_full_name', 'email']
admin.site.unregister(User)
admin.site.register(User, UserAdmim)
As you can see, one of the fields I want to be displayed in the model page listing -- defined by list_display
-- is get_full_name
. The problem is that the column label in the admin is displayed as Get full name.
如您所见,我希望在模型页面列表中显示的一个字段(由list_display定义)是get_full_name。问题是管理员中的列标签显示为获取全名。
My question is simple: can I override this? If so, how?
我的问题很简单:我可以覆盖这个吗?如果是这样,怎么样?
Thanks for your help.
谢谢你的帮助。
1 个解决方案
#1
31
Set an attribute in your function called short_description
to your desired label in your model definition.
在函数中将名为short_description的属性设置为模型定义中的所需标签。
# note, this must be done in the class definition;
# not User.get_full_name.short_description
get_full_name.short_description = 'my label'
Alternatively, if you don't want to pollute your model with admin specific code, you can set list_display
to a method on the ModelAdmin
which takes one argument: the instance. You'll also have to set readonly_fields
so that the admin doesn't try to look up this field in your model. I prefix admin fields with _
to differentiate.
或者,如果您不想使用管理员特定代码污染模型,可以将list_display设置为ModelAdmin上的一个方法,该方法接受一个参数:实例。您还必须设置readonly_fields,以便管理员不会尝试在模型中查找此字段。我在管理字段前加上_来区分。
class MyAdmin(...):
list_display = ('_my_field',)
readonly_fields = ('_my_field', )
def _my_field(self, obj):
return obj.get_full_name()
_my_field.short_description = 'my custom label'
Update:
Note that this will break default admin ordering. Your admin will no longer sort fields by clicking the label. To enable this functionality again, define an admin_order_field
.
请注意,这将破坏默认的管理员排序。您的管理员将不再通过单击标签对字段进行排序。要再次启用此功能,请定义admin_order_field。
def _date_created(self, obj):
return obj.date_created.strftime('%m/%d/%Y')
_date_created.short_description = "Date Created"
_date_created.admin_order_field = 'date_created'
Update 2:
I've written an admin method decorator that simplifies this process, because once I started using highly descriptive verbose method names, setting attributes on the function became massively repetitive and cluttering.
我编写了一个管理方法装饰器来简化这个过程,因为一旦我开始使用高度描述性的详细方法名称,在函数上设置属性变得大规模重复和混乱。
def admin_method_attributes(**outer_kwargs):
""" Wrap an admin method with passed arguments as attributes and values.
DRY way of extremely common admin manipulation such as setting short_description, allow_tags, etc.
"""
def method_decorator(func):
for kw, arg in outer_kwargs.items():
setattr(func, kw, arg)
return func
return method_decorator
# usage
class ModelAdmin(admin.ModelAdmin):
@admin_method_attributes(short_description='Some Short Description', allow_tags=True)
def my_admin_method(self, obj):
return '''<em>obj.id</em>'''
#1
31
Set an attribute in your function called short_description
to your desired label in your model definition.
在函数中将名为short_description的属性设置为模型定义中的所需标签。
# note, this must be done in the class definition;
# not User.get_full_name.short_description
get_full_name.short_description = 'my label'
Alternatively, if you don't want to pollute your model with admin specific code, you can set list_display
to a method on the ModelAdmin
which takes one argument: the instance. You'll also have to set readonly_fields
so that the admin doesn't try to look up this field in your model. I prefix admin fields with _
to differentiate.
或者,如果您不想使用管理员特定代码污染模型,可以将list_display设置为ModelAdmin上的一个方法,该方法接受一个参数:实例。您还必须设置readonly_fields,以便管理员不会尝试在模型中查找此字段。我在管理字段前加上_来区分。
class MyAdmin(...):
list_display = ('_my_field',)
readonly_fields = ('_my_field', )
def _my_field(self, obj):
return obj.get_full_name()
_my_field.short_description = 'my custom label'
Update:
Note that this will break default admin ordering. Your admin will no longer sort fields by clicking the label. To enable this functionality again, define an admin_order_field
.
请注意,这将破坏默认的管理员排序。您的管理员将不再通过单击标签对字段进行排序。要再次启用此功能,请定义admin_order_field。
def _date_created(self, obj):
return obj.date_created.strftime('%m/%d/%Y')
_date_created.short_description = "Date Created"
_date_created.admin_order_field = 'date_created'
Update 2:
I've written an admin method decorator that simplifies this process, because once I started using highly descriptive verbose method names, setting attributes on the function became massively repetitive and cluttering.
我编写了一个管理方法装饰器来简化这个过程,因为一旦我开始使用高度描述性的详细方法名称,在函数上设置属性变得大规模重复和混乱。
def admin_method_attributes(**outer_kwargs):
""" Wrap an admin method with passed arguments as attributes and values.
DRY way of extremely common admin manipulation such as setting short_description, allow_tags, etc.
"""
def method_decorator(func):
for kw, arg in outer_kwargs.items():
setattr(func, kw, arg)
return func
return method_decorator
# usage
class ModelAdmin(admin.ModelAdmin):
@admin_method_attributes(short_description='Some Short Description', allow_tags=True)
def my_admin_method(self, obj):
return '''<em>obj.id</em>'''