开发者

Chained reverse lookups in Django

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

Given an instance of [group categor开发者_Python百科y], 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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜