查询django ManyToMany [英] Querying django ManyToMany

查看:103
本文介绍了查询django ManyToMany的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

Foo < => FooGroup < => Bar

Foo <=> FooGroup <=> Bar

关系,其中< => 代表ManyToMany字段。

relation, where <=> stands for ManyToMany field.

如何检索所有 Foo 具体的 Bar 实例?

How do I retrieve all the Foos for a specific Bar instance?

推荐答案

关系非常类似于您的结构:用户< =>组< =>权限

Here's an example with auth models, where the relationship is very much like your structure : User <=> Groups <=> Permission

from django.contrib.auth import models
models.Permission.objects.filter(group__user=models.User.objects.get(username="webmaster"))

您的示例:

Foo.objects.filter(foogroup__bar=barinstance)

这篇关于查询django ManyToMany的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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