Allow user modify only objects created by this user

Viewed 1664

Is there a way in Django to give user access to admin privileges by allow only editing of objects that were created by this particular user?

For example I want to give the user permission to create other users but I don't want to allow them to modify users created by admin or other users with this privileges.

I'm using django-admin so the question is in context of it.

1 Answers

You should override the has_change_permission and has_delete_permission methods on your ModelAdmin class.

For example

class MyModelAdmin(admin.ModelAdmin):

    def has_change_permission(self, request, obj=None):
        if obj is not None and obj.created_by != request.user:
            return False
        return True

    def has_delete_permission(self, request, obj=None):
        if obj is not None and obj.created_by != request.user:
            return False
        return True
Related