1👍
✅
You can filter a Role queryset and follow the ManyToManyField relationship backwards using the name department
You can pass a queryset to the __in
filter to produce a nested query
def get_users_departments_roles(self):
"""Returns all the roles of all the departments the user is in."""
departments = self.get_departments()
return Role.objects.filter(department__in=departments).distinct()
Note however that certain databases (MySQL) don’t perform nested queries very well, see this warning at the end of this section in the docs
Source:stackexchange.com