Django 使用 contains 过滤多对多

发布于 2024-10-08 18:29:25 字数 1015 浏览 0 评论 0原文

我试图通过多对多关系过滤一堆对象。由于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暖心男生 2024-10-15 18:29:25

您是否尝试过类似的操作:

module.workflow_set.filter(trigger_roles__in=[self.role], allowed=True)

或者如果 self.role.id 不是 pks 列表:

module.workflow_set.filter(trigger_roles__id__exact=self.role.id, allowed=True)

Have you tried something like this:

module.workflow_set.filter(trigger_roles__in=[self.role], allowed=True)

or just if self.role.id is not a list of pks:

module.workflow_set.filter(trigger_roles__id__exact=self.role.id, allowed=True)
离鸿 2024-10-15 18:29:25

实现此目的的最简单方法是检查 ManyToManyField 中整个实例(而不是 id)的相等性。这会检查实例是否处于多对多关系内部。例子:

module.workflow_set.filter(trigger_roles=self.role, allowed=True)

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:

module.workflow_set.filter(trigger_roles=self.role, allowed=True)
佞臣 2024-10-15 18:29:25

我知道这是一个老问题,但看起来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, 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:

Event.objects.filter(eventgroups__in=u.groups.all())

望喜 2024-10-15 18:29:25

奇点与第一个例子几乎是正确的。您只需要确保它是一个列表。不过,第二个示例检查 trigger_roles__id__exact 是一个更好的解决方案。

module.workflow_set.filter(trigger_roles__in=[self.role.id],allowed=True)

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.

module.workflow_set.filter(trigger_roles__in=[self.role.id],allowed=True)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文