Python Django 过滤多对多包含
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4507893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django filter many-to-many with contains
提问by Grave_Jumper
I am trying to filter a bunch of objects through a many-to-many relation. Because the trigger_roles field may contain multiple entries I tried the contains filter. But as that is designed to be used with strings I'm pretty much helpless how i should filter this relation (you can ignore the values_list() atm.).
我试图通过多对多关系过滤一堆对象。因为 trigger_roles 字段可能包含多个条目,所以我尝试了 contains 过滤器。但是因为它被设计为与字符串一起使用,所以我非常无能为力,我应该如何过滤这种关系(您可以忽略 values_list() atm。)。
This function is attached to the user profile:
此功能附加到用户配置文件:
def getVisiblePackages(self):
visiblePackages = {}
for product in self.products.all():
moduleDict = {}
for module in product.module_set.all():
pkgList = []
involvedStatus = module.workflow_set.filter(trigger_roles__contains=self.role.id,allowed=True).values_list('current_state', flat=True)
My workflow model looks like this (simplified):
我的工作流模型如下所示(简化):
class Workflow(models.Model):
module = models.ForeignKey(Module)
current_state = models.ForeignKey(Status)
next_state = models.ForeignKey(Status)
allowed = models.BooleanField(default=False)
involved_roles = models.ManyToManyField(Role, blank=True, null=True)
trigger_roles = models.ManyToManyField(Role, blank=True, null=True)
Though the solution might be quiet simple, my brain won't tell me.
尽管解决方案可能很简单,但我的大脑不会告诉我。
Thanks for your help.
谢谢你的帮助。
采纳答案by mouad
Have you tried something like this:
你有没有尝试过这样的事情:
module.workflow_set.filter(trigger_roles__in=[self.role], allowed=True)
or just if self.role.idis not a list of pks:
或者只是如果self.role.id不是pks列表:
module.workflow_set.filter(trigger_roles__id__exact=self.role.id, allowed=True)
回答by Josh Smeaton
singularity is almost right with the first example. You just need to make sure it's a list. The second example, checking the trigger_roles__id__exactis a better solution though.
第一个例子的奇点几乎是正确的。你只需要确保它是一个列表。第二个例子,检查trigger_roles__id__exact是一个更好的解决方案。
module.workflow_set.filter(trigger_roles__in=[self.role.id],allowed=True)
回答by Caumons
The simplest approach to achieve this would be checking for equalty over the whole instance (instead of the id) in the ManyToManyField. That looks if the instance is inside the many to many relationship. Example:
实现此目的的最简单方法是检查ManyToManyField. 看看实例是否在多对多关系中。例子:
module.workflow_set.filter(trigger_roles=self.role, allowed=True)
回答by shacker
I know this is an old question, but it looks like the OP never quite got the answer he was looking for. If you have two sets of ManyToManyFields you want to compare, the trick is to use the __inoperator, not contains. So for example if you have an "Event" model with a ManyToMany to "Group" on field eventgroups, and your User model (obviously) attaches to Group, you can query like this:
我知道这是一个老问题,但看起来 OP 从来没有得到他想要的答案。如果您要比较两组 ManyToManyFields,诀窍是使用__in运算符,而不是contains. 因此,例如,如果您有一个“事件”模型,在 field 上将 ManyToMany 设置为“Group” eventgroups,并且您的 User 模型(显然)附加到 Group,您可以这样查询:
Event.objects.filter(eventgroups__in=u.groups.all())
Event.objects.filter(eventgroups__in=u.groups.all())

