Django

Code

Ticket #5324 (closed: fixed)

Opened 9 months ago

Last modified 3 weeks ago

queryset exclude fails across a many-to-many list

Reported by: Robert Bunting <robert@exoweb.net> Assigned to: nobody
Component: Core framework Version: SVN
Keywords: exclude m2m many-to-many, qs-rf-fixed Cc:
Triage Stage: Accepted Has patch: 0
Needs documentation: 0 Needs tests: 0
Patch needs improvement: 0

Description (Last modified by mtredinnick)

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.

Attachments

Change History

09/03/07 02:51:05 changed by mtredinnick

  • owner changed from adrian to mtredinnick.
  • needs_better_patch changed.
  • description changed.
  • needs_tests changed.
  • needs_docs changed.

(Tweaked ticket formatting slightly).

09/13/07 16:45:24 changed by mtredinnick

  • keywords changed from exclude m2m many-to-many to exclude m2m many-to-many, qs-rf.
  • stage changed from Unreviewed to Accepted.

10/14/07 20:21:11 changed by mtredinnick

  • keywords changed from exclude m2m many-to-many, qs-rf to exclude m2m many-to-many, qs-rf-fixed.

04/26/08 21:50:16 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(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


Add/Change #5324 (queryset exclude fails across a many-to-many list)




Change Properties
Action