Django - 仅限超级用户使用Admin list_filter?

时间:2011-05-26 23:09:40

标签: django django-admin-filters

是否可以仅为超级用户显示list_filter?

2 个答案:

答案 0 :(得分:6)

Override changelist view

存储原始列表过滤器的副本,以便您可以动态设置每个请求的属性。检查用户是否是超级用户,并设置列表过滤器属性。

class MyAdmin(admin.ModelAdmin):
    list_filter = ('id',)
    _list_filter = list_filter
    # must store list filter reference somewhere

    def changelist_view(self, request, extra_context=None):    
        if not request.user.is_superuser:
            self.list_filter = None
        else:
            self.list_filter = self._list_filter
        return super(MyAdmin, self).changelist_view(request, extra_context)

答案 1 :(得分:0)

自Django 1.5起,您可以使用ModelAdmin.get_list_filter

class MyAdmin(admin.ModelAdmin):
    def get_list_filter(self, request):
        if request.user.is_superuser:
            return ['field']
        else:
            return []