Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display different list_display depends on user [closed]

I have a model include some columns: A,B,C.When the logger is superuser,the list_display will includes all the columns(A,B,C),otherwise,other user can only see part of the columns,for example:B and C.Most of the related answer is user get_form(...),but this is alter the 'exclude','fields' and 'fieldsets',I want alter the list_dispaly.

like image 415
Richard Yang Avatar asked Dec 16 '22 05:12

Richard Yang


1 Answers

You would use the changelist_view method to edit list_display:

class MyModelAdmin(admin.ModelAdmin):
    list_display = ('A', 'B', 'C',)

    def changelist_view(self, request, extra_context=None):
        if not request.user.is_superuser:
            self.list_display = ('B', 'C',)
        return super(MyModelAdmin, self).changelist_view(request, extra_context)
like image 130
hellsgate Avatar answered Feb 01 '23 01:02

hellsgate