QuerySet: LEFT JOIN with AND

I am using an old version of Django 1.1 with a hack that supports connection in extra (). It works, but now is the time for change. Django 1.2 uses RawQuerySet, so I rewrote my code for this solution. The problem is that RawQuery does not support filters, etc., which I have a lot in the code. Digging through Google, in CaktusGroup I found that I can use query.join (). It would be great, but in the code I have:

LEFT OUTER JOIN "core_rating" ON ("core_film"."parent_id" = "core_rating"."parent_id" AND "core_rating"."user_id" = %i 

In query.join (), I wrote the first part of "core_film"."parent_id" = "core_rating"."parent_id" , but I don’t know how to add the second part after AND.
Is there any solution for Django that I could use custom JOINs without overwriting all the filter code (Raw)?

This is our current code snippet in extra ()

 top_films = top_films.extra( select=dict(guess_rating='core_rating.guess_rating_alg1'), join=['LEFT OUTER JOIN "core_rating" ON ("core_film"."parent_id" = "core_rating"."parent_id" and "core_rating"."user_id" = %i)' % user_id] + extra_join, where=['core_film.parent_id in (select parent_id from core_film EXCEPT select film_id from filmbasket_basketitem where "wishlist" IS NOT NULL and user_id=%i)' % user_id, '( ("core_rating"."type"=1 AND "core_rating"."rating" IS NULL) OR "core_rating"."user_id" IS NULL)', ' "core_rating"."last_displayed" IS NULL'], ) 
+7
source share
2 answers

Unfortunately, the answer is no.

Django ORM, like most Django, follows the philosophy that light things should be light and difficult things should be possible. In this case, you are definitely in the "hard stuff" area, and the "possible" solution is to simply write a raw request. There are certain situations like this one where recording an unprocessed request can be complex and looks rough, but from a project perspective, such situations are too rare to justify the cost of adding such functions.

+2
source
0
source

All Articles