Last active
May 25, 2021 14:06
-
-
Save andybak/806a0c5e34df98df9337 to your computer and use it in GitHub Desktop.
Django ORM subquery example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT * FROM "logistics_consignment" | |
WHERE NOT ("logistics_consignment"."id" IN | |
(SELECT U0."id" | |
FROM "logistics_consignment" U0 | |
LEFT OUTER JOIN "office_split" U1 ON (U0."id" = U1."departure_id") | |
LEFT OUTER JOIN "office_booking" U2 ON (U1."booking_id" = U2."id") | |
LEFT OUTER JOIN "office_split" U3 ON (U0."id" = U3."arrival_id") | |
LEFT OUTER JOIN "office_booking" U4 ON (U3."booking_id" = U4."id") | |
WHERE (U2."status" = quote | |
OR U4."status" = quote))) | |
ORDER BY "logistics_consignment"."time_at_start" ASC |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
q = Consignment.objects.filter( | |
Q(split_departure_set__booking__status='quote') | | |
Q(split_arrival_set__booking__status='quote') | |
) | |
c = Consignment.objects.exclude(pk__in=q) | |
# Test it | |
print [x.content_object.status for x in c] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment