demonno
demonno

Reputation: 524

Django model filter avoid join when filtering by relation id

Django ORM query

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

This generates following sql query, [join used]

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

Is it possible to avoid join and get result like this?

""""
select * 
FROM "project" 
WHERE "project"."category_id" = 1111
""""

Upvotes: 0

Views: 164

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599620

The underlying db column is called category_id (with a single underscore); you can filter on that directly:

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

Upvotes: 1

Related Questions