开发者

Django - Admin list_filter only for superuser?

开发者 https://www.devze.com 2023-03-08 12:58 出处:网络
Is it 开发者_如何学Cpossible to show list_filter only for superuser?Override changelist view Store a copy of the original list filter so that you can dynamically set the attribute per-request. Check i

Is it 开发者_如何学Cpossible to show list_filter only for superuser?


Override changelist view

Store a copy of the original list filter so that you can dynamically set the attribute per-request. Check if the user is a superuser, and set the list filter property or not.

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)


Since Django 1.5, you can use ModelAdmin.get_list_filter:

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

精彩评论

暂无评论...
验证码 换一张
取 消