Richard
Richard

Reputation: 1307

Chained reverse lookups in Django

I have [profile] --M2M--> [group] --FK--> [group category].

Given an instance of [group category], I need to retrieve all related [profile].

(In english: I have members belonging to one or more groups, which are in categories. I need to find all the members in a given category of group).

How do I span the ForeignKey and ManytoMany keys in between? No matter how I slice this, I always end up with an expression from which I can't define the next backward relationship.

Thank you.

Upvotes: 0

Views: 64

Answers (1)

Rob Osborne
Rob Osborne

Reputation: 5005

Assuming something like:

object Profile():
    groups = models.ManyToManyField('Group')

object Group():
    category = models.ForeignKey('GroupCategory')

You should be able to just query it:

profiles = Profile.objects.filter(groups__category=thegroupcategory)

Upvotes: 1

Related Questions