Django left join with and condition

41 views
Skip to first unread message

chirag soni

unread,
Jun 3, 2019, 8:44:14 AM6/3/19
to django...@googlegroups.com

I have 3 models Product,Photo,ProductLikeDislike. I am left joining all the three. For that I wrote this query:

x=Product.objects.values_list('name','photo','productlikedislike')


Through this I am getting correct left join I printed and checked like this:

Noteolx is the name of my Django app.

print(x.query)

SELECT "olx_product"."name", "olx_photo"."id", "olx_productlikedislike"."id"
 FROM "olx_product" LEFT OUTER JOIN "olx_photo" ON ("olx_product"."id" = 
"olx_photo"."reference_id_id") LEFT OUTER JOIN "olx_productlikedislike" ON 
("olx_product"."id" = "olx_productlikedislike"."product_id_id") ORDER BY 
"olx_product"."created" DESC

Now I want to add extra and condition along with ON statement like this:

ON ("olx_product"."id" = 
    "olx_productlikedislike"."product_id_id"
and "olx_productlikedislike"."product_liked_by_id"=2)

So for this somebody suggested me that use Django's FilteredRelation. I used but it is not adding extra and condition along with ON

I used FilteredRelation like this:

x=Product.objects.annotate( 
productlikedislike_product_liked_by_id=FilteredRelation('productlikedislike',
condition=Q(productlikedislike__product_liked_by_id=2))).values_list('name',
'photo','productlikedislike')

but getting the same sql query no extra and condition. I am using Django 2.1.5

Reply all
Reply to author
Forward
0 new messages