【发布时间】:2025-11-28 05:35:02
【问题描述】:
具有以下型号:
class OrderOperation(models.Model):
ordered_articles = models.ManyToManyField(Article,
through='orders.OrderedArticle')
class OrderedArticle(models.Model):
order_operation = models.ForeignKey(OrderOperation)
article = models.ForeignKey(Article)
articles = ... # some queryset containing multiple articles
如果我想查找至少包含一篇文章的订单操作,这将按预期工作:
OrderOperation.objects.filter(ordered_articles__in=articles)
但是,如果我想用订单中的所有文章查找订单操作,正确的方法是什么?
OrderOperation.objects.filter(ordered_articles=articles) 引发 ProgrammingError: more than one row returned by a subquery used as an expression 错误(实际上我明白为什么)。
【问题讨论】:
标签: python django many-to-many django-orm