#19848: ANDed Q filters spanning generic relations evaluate unexpectedly to 
empty
querie sets
-------------------------------------+-------------------------------------
     Reporter:  gotoalanlu@…         |                    Owner:  nobody
         Type:  Bug                  |                   Status:  closed
    Component:                       |                  Version:  master
  contrib.contenttypes               |               Resolution:  invalid
     Severity:  Normal               |             Triage Stage:
     Keywords:  generic              |  Unreviewed
  genericrelations filter q and      |      Needs documentation:  0
    Has patch:  0                    |  Patch needs improvement:  0
  Needs tests:  0                    |                    UI/UX:  0
Easy pickings:  0                    |
-------------------------------------+-------------------------------------

Comment (by Alan Lu <gotoalanlu@…>):

 Apologies: my original post contained an error. I meant to refer to the
 distributive property of AND and OR. Therefore, in my original line of
 thought on the workaround, queries like

 {{{
 query = (Q(objs__name = "foo") & ~Q(objs__name = "bar")) | Q(objs__name =
 "baz")
 ExampleModel.filter(query)
 }}}

 would instead be written as

 {{{
 query1 = Q(objs__name = "foo") | Q(objs__name = "baz")
 query2 = ~Q(objs__name = "bar") | Q(objs__name = "baz")
 ExampleModel.filter(query1).filter(query2)
 }}}

 though DeMorgan's laws could certainly still be useful when unwrapping
 deeply nested logical statements with NOTs on parenthetical groups.

 The intent of the code is to query for instances of a model with either a
 relationship with an object "foo" and no relationship with an object "bar"
 or a relationship with an object "baz."

 Because I was unaware of the way SQL handles these relationships, I
 misjudged the capacity of the system, so it appears that the workaround is
 natural. Having read about SQL joins, I am now convinced that Django is
 working as designed, even though I am a bit surprised.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/19848#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to