简体   繁体   中英

Django - Admin list_filter only for superuser?

Is it possible 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 []

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM