Search code examples
djangodjango-modelsdjango-admindjango-admin-filters

Hide certain fields in Django admin site for different user


I have an admin site that I need to open up to more admins.

Currently my model looks like

class YouTube(models.Model):
    name =  models.CharField(max_length=100)
    credit_card_number = models.CharField(max_length=100)

Is there a way in the admin site frame work to make it so that only superusers can see the credit card number? In the admin site framework, I can only see the ability to add, edit, delete.


Solution

  • Create method YouTube.get_cc_root_only, where you are to check if user is root, and use it in YouTubeAdmin class (list_display)

    UPDATED:

    class XyzAdmin(admin.ModelAdmin):
        def get_cc_root_only(self, obj):
            if self.username == "admin":
                return "CC"
            return "XXX"
    
        def changelist_view(self, request, extra_context = None):
            self.username = request.user.username
            return super(XyzAdmin,self).changelist_view(request, extra_context = extra_context)
    
        list_display = ("name", "get_cc_root_only")