我想通过多对多关系过滤一堆对象。因为trigger_roles字段可能包含多个条目,所以我尝试了包含过滤器。但是,因为这是设计用于字符串我非常无奈,我应该如何过滤这个关系(你可以忽略values_list()atm。)。Django过滤器多对多包含
该功能安装到用户配置文件:
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)
尽管解决方案可能是简单安静,我的大脑不会告诉我。
感谢您的帮助。
不似乎工作。由于self.role.id只是一个int,而trigger_roles是它们的列表,所以我需要一个倒置的,就像我发现的那样,contains只包含字符串。 – 2010-12-22 11:24:53