Hide certain fields in Django admin site for different user

寵の児 提交于 2019-12-08 02:02:10

问题


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.


回答1:


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")


来源:https://stackoverflow.com/questions/10356491/hide-certain-fields-in-django-admin-site-for-different-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!