1

I have two different groups who are allowed to access Django-Admin. They are allowed to view all Fields in a Model, but shall not edit all Field. For example G1 is allowed to edit "price" and "ean" whilst G2 is allowed to edit "ean" and "detail".

I tried the version given here, but I also want to restrict the editability in the detail view of a model object.

EDIT: currently, I'm trying to figure out how to access the request user in a ModelAdmin, as I've registered the "special-field-permissions" in the Meta of the model and am trying to modify the readonly_fields depending on the group. any idea how to get the user?

  • 1
    Consider adding what you've tried so far into the original question. SO works best if you provide more code and ask us to debug. we can't provide code for you. – Raja Simon Feb 26 '20 at 19:09

1 Answers1

0

the solution i came up with is to overwrite get_changelist and get_form:

def get_form(self, request, obj=None, **kwargs):
    fields = [$$$]  # Default fields for everyone
    if not has_group(request.user, $$$):  # Check if User IS NOT in group
        fields += [$$$]  # Add field that is associated with group to list
    self.readonly_fields = fields
    return super(YourAdminName, self).get_form(request, obj, **kwargs)

def get_changelist(self, request, **kwargs):
    fields = [$$$]  # Default fields for everyone
    if has_group(request.user, $$$):  # Check if User IS in group
        fields += [$$$]  # Add field that is associated with group to list
    self.list_editable = fields
    return super(YourAdminName, self).get_changelist(request, **kwargs)