如何动态地指定“list_display”一个django ModelAdmin类的属性? [英] How can I dynamically specify the "list_display" attribute of a django ModelAdmin class?
问题描述
在尝试动态更改django管理员模型列表页面中显示的列时,我尝试将我的ModelAdmin类的 __ init __()
方法重写为根据当前用户的权限,从list_display属性动态添加或删除特定字段。但是,我发现ModelAdmin类只能在每次重新启动时实例化一次,因此不起作用。
In trying to dynamically change the columns that are displayed in the model-list page of the django admin, I tried overriding the __init__()
method of my ModelAdmin class to dynamically add or remove a particular field from the list_display attribute, depending on the permissions of the current user. However, I found that ModelAdmin classes are only instantiated once per restart, so that doesn't work...
是否有另一种方式动态更改list_display字段? / p>
Is there another way to dynamically change the list_display field?
推荐答案
在提出这个问题时,我偶然发现了答案,所以我以为我会分享...
While asking this question, I stumbled across the answer, so I thought I'd share...
票据#14206 表示此功能已添加到django前一段时间(1.4版,我相信)。 ModelAdmin类现在支持get_list_display()方法:
Ticket #14206 indicates that this feature was added to django some time ago (version 1.4, I belive). ModelAdmin classes now support a get_list_display() method:
def get_list_display(self, request):
if request.user.has_perm('my_app.my_permission'):
list_display = ('field_1', 'field_2', 'dynamic_field',)
else:
list_display = ('field_1', 'field_2',)
return list_display
这篇关于如何动态地指定“list_display”一个django ModelAdmin类的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!