122👍
✅
company.user_set.all()
will return a QuerySet of User
objects that belong to a particular company. By default you use modelname_set
to reverse the relationship, but you can override this be providing a related_name
as a parameter when defining the model, i.e.
class User(models.Model):
companies = models.ManyToManyField(Company, ..., related_name="users")
> company.users.all()
Source:stackexchange.com