Django中的链式反向查找 [英] Chained reverse lookups in Django

查看:134
本文介绍了Django中的链式反向查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 [个人资料] --M2M - > [group] --FK - > [group category] ​​

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

给定一个 [group category] ​​,我需要检索所有相关的 [profile]

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

如何跨越ForeignKey和ManytoMany键在中间?无论我如何划分这个,我总是得到一个表达,我从中不能定义下一个向后关系。

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.

谢谢。

推荐答案

假设如下:

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

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

您应该能够查询:

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

这篇关于Django中的链式反向查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆