Opened 17 years ago
Closed 17 years ago
#5324 closed (fixed)
queryset exclude fails across a many-to-many list
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Core (Other) | Version: | dev |
Severity: | Keywords: | exclude m2m many-to-many, qs-rf-fixed | |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
If you try to exclude items from a queryset across a m2m link, it fails to give the expected result.
For instance, assuming I have users, some of whom are in group Editors, some in group Sales, and most are not in a group at all.
from django.contrib.auth.models import User user_qs = User.objects.all() # all users non_editor_users = user_qs.exclude(groups__name='Editors') # should be all users excluding those in group Editors
The set wrongly returned is actually the set of users who are in Sales but not in Editors - it fails to include those users who have no group at all.
The reason is the sql clause generated, which is doing
FROM "auth_user" LEFT OUTER JOIN "auth_user_groups" AS "m2m_auth_user__groups" ON "auth_user"."id" = "m2m_auth_user__groups"."user_id" INNER JOIN "auth_group" AS "auth_user__groups" ON "m2m_auth_user__groups"."group_id" = "auth_user__groups"."id" WHERE ((NOT ("auth_user__groups"."name" = %s))) ORDER BY "auth_user"."username" ASC', ['Editors']
A workaround for this was posted some time back in http://groups.google.com/group/django-users/browse_thread/thread/81eb405071037248/2beb073a93fd68b7
I can't find a ticket for it - please excuse me if it's a duplicate. I guess #2091 might be related under the hood.
Change History (4)
comment:1 by , 17 years ago
Description: | modified (diff) |
---|---|
Owner: | changed from | to
comment:2 by , 17 years ago
Keywords: | qs-rf added |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 17 years ago
Keywords: | qs-rf-fixed added; qs-rf removed |
---|
comment:4 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
(In [7477]) Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
(Tweaked ticket formatting slightly).