如何动态指定“list_display"?django ModelAdmin 类的属性? [英] How can I dynamically specify the "list_display" attribute of a django ModelAdmin class?

查看:22
本文介绍了如何动态指定“list_display"?django ModelAdmin 类的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试动态更改在 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 字段吗?

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...

Ticket #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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆