0

I have model where users add organizations, how can restrict their access to only organizations created by the user? (restriction include view and edit).

Example:

User1 add Organization1

User2 add Organization2

User1 must see and edit only Organization1

User2 must see and edit only Organization2

models.py

class Organization (models.Model):
    enumerator_name = models.ForeignKey(User)
    org_name = models.CharField(max_length=255)

admin.py

class OrganizationAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        obj.enumerator_name = request.user
        obj.save()
Ahmad Alzoughbi
  • 474
  • 1
  • 5
  • 14

1 Answers1

0

Solved, not sure if it's the correct way, but it works just as i wanted.

class OrganizationAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        obj.enumerator_name = request.user
        obj.save()

    def get_queryset(self, request):
        method = getattr(
            super(PeopleAdmin, self), 'get_queryset',
            getattr(super(PeopleAdmin, self), 'queryset', None))
        qs = method(request)
        return qs.filter(enumerator_name=request.user)
Ahmad Alzoughbi
  • 474
  • 1
  • 5
  • 14