Search code examples
pythondjangodatabase-designdjango-modelsdjango-guardian

Model design for a user who can have roles at multiple organizations


I need a Django model where a User can have a Function with any number of Organisations. His permissions (change, view, delete) with the organization are determined by his Role. I am pretty sure I only need an "admin" and a "member" role.

This would require row-level permissions, so I decided to go with django-guardian. I am having trouble choosing the proper model design. These are the alternatives

enter image description here

The first one would have the advantage of creating new roles, but I don't think I need that. Also I can enforce unique_together so that a User can only have 1 function at every company. Would I set the can_change permission at the Role and infer the row level permission based on the relation between User and Organization? The would mean I do not even need django-guardian, right?

The second one looks much simpler, but maybe that is deceptive. The permissions would have to be set as soon as a User is added to an Organization and are definitely row-level.

What is the right strategy here?

To clarify: in both cases a user can be an admin of one organization and simply a member of another organization.


Solution

  • Use the Party Model.

    A user is not a person, it's a user. Person and organization are parties. A party hasOne (or no) user.

    A person hasMany (many2many) relationships with an organization:

    Individual -< Relationship >- Organization

    Organizations can have relationships with each other too.