I think you could use the django-rules library here. Link
This is a rule-based mechanism, very similar to decision trees, and can be easily integrated with the frameworksclass framework DRF.
The best part is that you can perform operations on simple permissions and create complex permissions from them.
Example
>>> @rules.predicate >>> def is_admin(user): ... return user.is_staff ... >>> @rules.predicate >>> def is_object_owner(user, object): return object.owner == user
Predicates can do almost anything with given arguments, but should always return True if the condition they are checking is true, False otherwise. Now combine these two predicates.
is_object_editable = is_object_owner | is_admin
You can use this new predicate rule is_object_editable inside your has_permissions permission class method.
source share