Opened 10 years ago

Last modified 10 years ago

#23589 closed Bug

Django 1.7 filter Q m2m bug — at Version 1

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

Description (last modified by Tim Graham)

The behavior of Q objects has changed in an undocumented and breaking way for m2m relationships in Django 1.7 from the behavior in 1.6.7, returning different query results depending on the Django version.

In the case where you want to select a primary object that has related objects of one type, but none of another, you could use a Q like this:
q = Q(Q(relatedobject__someparam=True), ~Q(relatedobject__someparam=False))
results = PrimaryObject.objects.filter(q)

Using this Q in a filter in 1.6.7 would give you all primary objects that have relatedobjects with someparam=True and omit any primary objects that also had a relatedobject with someparam=False. In 1.7 these objects are returned despite the ~Q(relatedobject__someparam=False).

This gist provides a sample to illustrate the problem:
https://gist.github.com/scottsexton/375f7869839a98593695

Change History (1)

comment:1 by Tim Graham, 10 years ago

Description: modified (diff)

It would be helpful if you could create a test for Django's test suite that demonstrates the issue (ideally reusing existing models if you can find appropriate ones) and bisect to the commit that introduced the regression.

Note: See TracTickets for help on using tickets.
Back to Top