Home > OS >  Reverse query via foreign key and many to many field in Django
Reverse query via foreign key and many to many field in Django

Time:04-15

What query can I use to return all the Organisation objects that are members of a certain membership_id value?

e.g. return all Organisation objects that have a membership_id = 101

class Organisations(model)
    id = models.CharField()
    memberships = models.ManyToManyField("self", through="Memberships")

class Memberships(model)
    membership_id = models.IntegerField()
    organisation_id = models.ForeignKey(Organisations, related_name="orgs",)

CodePudding user response:

Try this:

Organisations.objects.filter(orgs__membership_id=101)
  • Related