Django how to define permissions so users can only edit certain model hierarchies?
<p>If I have models like this..</p>

<pre><code>class Family(Model):
    name = models.CharField()

class Father(Model):
    family = ForeignKey(Family)

class Mother(Model):
    family = ForeignKey(Family)

class Child(Model):
    family = ForeignKey(Family)
</code></pre>

<p>Django makes group permissions automatically so I can define groups that can edit/create/etc... the family model.</p>

<p>How can I limit it to only let a user edit a certain instance of the family model? So if I want the 'Johnson' family admin to only have permission to edit things under the 'Johnson' family tree. </p>

<p>I can think of two ways, one would be defining a custom permission like (<a href="https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#custom-permissions" rel="nofollow">https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#custom-permissions</a>) but I have no idea what the docs are saying there and they do not do a good job of explaining it. </p>

<p>I can also think of possibly adding a field on the user model and checking the value of that, but it feels wrong to do it that way...</p>

<p>You can use django modules that provide this functionality.
I personally use <a href="https://github.com/django-guardian/django-guardian" rel="nofollow">django-guardian</a> which offers per-object permissions using an interface similar to the original <code>has_perm</code> method.</p>

python django