【发布时间】:2022-01-14 05:12:20
【问题描述】:
以书籍和作者模型为例,书籍拥有一位或多位作者。具有cover_type 的书籍和以国家为原籍的作者。
如何列出所有精装书籍,以及作者来自法国的情况?
Books.objects.filter(cover_type='hard', authors__origin='france')
此查询不检索精装但没有法国作者的书籍。
我想要所有精装书,这是谓词#1。
如果他们的作者来自法国,我希望他们注释,否则作者字段可能为空或“无”。
例如。:
`
Bookname, covertype, origin
The Trial, hardcover, none
Madam Bovary, hardcover, France
`
尝试了许多选项,注释、Q、值、子查询、何时、案例、存在但可以提出解决方案。
使用 sql 就这么简单:
select * from books b left join authors a on a.bookref=b.id and a.origin=france where b.covertype='hard'
(我的模型不是书籍和作者,我选择它们是因为它们是 django-docs 的示例模型。我的模型是 building 和 buildingtype,我想要 building.id=454523 和 buildigtype,其中 buildingtype 处于活动状态,buildingtype 可能为 null建筑物或只有 1 个主动和零个或多个被动)
【问题讨论】: