Django 使用 contains 过滤多对多
我试图通过多对多关系过滤一堆对象。由于 trigger_roles
字段可能包含多个条目,因此我尝试了 contains
过滤器。但由于它被设计为与字符串一起使用,我非常无助,我应该如何过滤这个关系(您可以忽略 values_list()
atm。)。
此功能附加到用户配置文件:
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)
我的工作流程模型如下所示(简化):
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)
虽然解决方案可能非常简单,但我的大脑不会告诉我。
感谢您的帮助。
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.).
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否尝试过类似的操作:
或者如果 self.role.id 不是 pks 列表:
Have you tried something like this:
or just if
self.role.id
is not a list of pks:实现此目的的最简单方法是检查
ManyToManyField
中整个实例(而不是 id)的相等性。这会检查实例是否处于多对多关系内部。例子: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:我知道这是一个老问题,但看起来OP从未完全得到他正在寻找的答案。如果您想要比较两组 ManyToManyFields,技巧是使用 __in 运算符,而不是 contains。因此,例如,如果您有一个“Event”模型,在字段
eventgroups
上有一个 ManyToMany 到“Group”,并且您的 User 模型(显然)附加到 Group,您可以像这样查询:Event .objects.filter(eventgroups__in=u.groups.all())
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
__in
operator, notcontains
. So for example if you have an "Event" model with a ManyToMany to "Group" on fieldeventgroups
, and your User model (obviously) attaches to Group, you can query like this:Event.objects.filter(eventgroups__in=u.groups.all())
奇点与第一个例子几乎是正确的。您只需要确保它是一个列表。不过,第二个示例检查
trigger_roles__id__exact
是一个更好的解决方案。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__exact
is a better solution though.