Created
December 24, 2019 15:41
-
-
Save filipeximenes/c43b138bb9841848205c6589e3a486d8 to your computer and use it in GitHub Desktop.
Django AND query
This file contains hidden or 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
| User.objects.filter( | |
| Q(internal_groups__id=1) & | |
| ~Q( | |
| enrollments__event__id=15, | |
| enrollments__pre_status=ENROLLMENT_PRE_STATUS.going, | |
| ) | |
| ) |
This file contains hidden or 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 "users_user" | |
| INNER JOIN "internalgroups_internalgroupuser" | |
| ON ( "users_user"."id" = | |
| "internalgroups_internalgroupuser"."user_id" ) | |
| WHERE ( "internalgroups_internalgroupuser"."internal_group_id" = 1 | |
| AND NOT ( "users_user"."id" IN (SELECT U1."user_id" | |
| FROM "events_enrollment" U1 | |
| WHERE U1."event_id" = 15) | |
| AND "users_user"."id" IN (SELECT U1."user_id" | |
| FROM "events_enrollment" U1 | |
| WHERE U1."pre_status" = going) ) ) |
This file contains hidden or 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
| enrollments = Subquery(Enrollment.objects.filter( | |
| event__id=15, pre_status=ENROLLMENT_PRE_STATUS.going | |
| ).only('id')) | |
| User.objects.filter( | |
| Q(internal_groups__id=1) & | |
| ~Q(enrollments__in=enrollments) | |
| ) |
This file contains hidden or 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 "users_user" | |
| INNER JOIN "internalgroups_internalgroupuser" | |
| ON ( "users_user"."id" = | |
| "internalgroups_internalgroupuser"."user_id" ) | |
| WHERE ( "internalgroups_internalgroupuser"."internal_group_id" = 1 | |
| AND NOT ( "users_user"."id" IN ( | |
| SELECT V1."user_id" | |
| FROM "events_enrollment" V1 | |
| WHERE V1."id" IN ( | |
| SELECT U0."id" | |
| FROM "events_enrollment" U0 | |
| WHERE ( | |
| U0."event_id" = 15 | |
| AND | |
| U0."pre_status" = going | |
| ), | |
| ) ) ) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment