Opened 10 years ago

Closed 7 years ago

#13198 closed Bug (fixed)

exclude() bug when used with Q() objects

Reported by: mucisland Owned by: nobody
Component: Database layer (models, ORM) Version: 1.1
Severity: Normal Keywords: exclude Q
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


.exclude() behaves different when its argument is wrapped into Q(). Specifically, when wrapping an exclude filter with Q(), objects containing NULL in related fields are excluded as well as objects which have matching content in this field.

The SQL which is generated by exclude(<field>=<value>) should _not_ match objects with NULL in the respective field, but this is sometimes violated when wrapping the filter with Q(). (So far I've only seen this bug when using a filter which specifies a field in a related object.)

The attached file contains a minimum Django project (project name is "sandbox", application name is "q") with two test cases which should clarify the described behaviour.

Best Regards,

Attachments (1) (6.3 KB) - added by mucisland 10 years ago.
Django project "sandbox" with app "q" which reproduces the bug. Execute " test".

Download all attachments as: .zip

Change History (6)

Changed 10 years ago by mucisland

Attachment: added

Django project "sandbox" with app "q" which reproduces the bug. Execute " test".

comment:1 Changed 10 years ago by Russell Keith-Magee

Resolution: duplicate
Status: newclosed

I'm fairly certain that this is a duplicate of #11052; one of the examples in the comments seems to match up.

comment:2 Changed 7 years ago by jedediah@…

Easy pickings: unset
Severity: Normal
Type: Uncategorized
UI/UX: unset

Still seeing this in Django 1.4, so either it was erroneously marked as a dupe or it snuck back in. It's pretty simple to reproduce:

MyModel.objects.exclude(nullable_field=123)   => includes rows with nullable_field=null
MyModel.objects.exclude(Q(nullable_field=123))   => excludes rows with nullable_field=null

comment:3 Changed 7 years ago by Anssi Kääriäinen

Resolution: duplicate
Status: closednew
Triage Stage: UnreviewedAccepted

I tried this with master, one of the tests pass, the other doesn't. After the patch to #17600 this one would be fixed this.

I'm reopening this one so that proper test will be added in #17600.

comment:4 Changed 7 years ago by Anssi Kääriäinen

Type: UncategorizedBug

comment:5 Changed 7 years ago by Anssi Kääriäinen

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top