我有用户添加组织的模型,如何限制他们只访问用户创建的组织? (限制包括查看和编辑)。
示例:
User1添加Organization1
User2添加Organization2
User1必须只能查看和编辑Organization1
User2必须只能查看和编辑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()
答案 0 :(得分:0)
解决了,不确定它是否是正确的方法,但它可以按照我想要的方式工作。
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)