Reputation: 2554
Is it possible to show list_filter only for superuser?
Upvotes: 2
Views: 3643
Reputation: 6576
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 []
Upvotes: 4
Reputation: 118448
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)
Upvotes: 6