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:
Note: olx 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