[Django]-How to use the 'reverse' of a Django ManyToMany relationship?

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()

here is the relevant documentation

Leave a comment