根据该表上的另一个字段从相关表中注释字段

时间:2017-11-21 08:01:44

标签: python mysql django filter annotations

让我们说我有两张桌子" Publisher"和#34; Book"。本书通过Foreign-Key与Publisher相关。为了能够从出版商那里获得书籍,有一个related_name =" Book"在书模型中。如何基于另一个字段(例如图书的价格)将特定图书的日期注释到我对发布者的查询? 在我看来是这样的:

Publisher.objects.annotate(
     particular_date = F('Book__date' for which the price=150$)
).all()

我希望我能够实现这个想法。

1 个答案:

答案 0 :(得分:2)

对于复杂的注释,您还需要指定注释名称,因此在我看来它将如下所示:

Publisher.objects.filter(Book__price=150).\
     annotate(particular_date = F('Book__date')).all()
相关问题