Querying django ManyToMany

孤街醉人 提交于 2019-12-11 07:57:55

问题


I have got

Foo <=> FooGroup <=> Bar

relation, where <=> stands for ManyToMany field.

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


回答1:


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

With your example:

Foo.objects.filter(foogroup__bar=barinstance)


来源:https://stackoverflow.com/questions/1388137/querying-django-manytomany

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!