Django模型过滤器在按关系id过滤时避免连接

时间:2018-03-21 11:19:43

标签: sql django join orm

Django ORM查询

Future task started scala-execution-context-global-11
Completed some task by main
false
Completed some task by main
false
Completed some task by main
false
Future task is still pending and main thread have no more lines to execute

这会生成以下sql查询,[join used]

projects = Project.objects.filter(category__id=1111)

是否可以避免加入并得到这样的结果?

""""
select * 
FROM "project" 
INNER JOIN "category" ON ( "project"."category_id" = "category"."id" ) 
WHERE "project"."category_id" = 1111
""""

1 个答案:

答案 0 :(得分:0)

底层db列名为category_id(带有一个下划线);你可以直接过滤:

projects = Project.objects.filter(category_id=1111)