直接上代码:
注:FilteredRelation 不能使用嵌套查询。
from django.db.models import Q,FilteredRelation
class Books(Model):
id = ...
book_name = ...
author = models.ForeignKey('apps.Author')
class Author(Model):
id = ...
author_name = ...
sex = ...
Author.objects.annotate(b = FilteredRelation("books", condition=Q(books__book_name = "盗墓笔记"))).filter(id = 10)
SELECT
*
FROM
author
LEFT JOIN books b ON b.`author` = author.id AND b.book_name = "盗墓笔记"
WHERE
author.id = 10
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:django-orm django-orm 使链接查询(LEFT JOIN)的条件 写在 ON 之后,而不是 WHERE - Python技术站