Django - Admin list_filter only for superuser?
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 []
精彩评论