#22709: Pproblem with negating Q object
----------------------------------------------+--------------------
     Reporter:  thierryb@…                    |      Owner:  nobody
         Type:  Uncategorized                 |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  1.6
     Severity:  Normal                        |   Keywords:
 Triage Stage:  Unreviewed                    |  Has patch:  0
Easy pickings:  0                             |      UI/UX:  0
----------------------------------------------+--------------------
 Hello,

 I have 2 models. One is the computer model which has a M2M field linking
 to an application model.

 If I want to know the computers that have a specific app I can for
 instance do:
 Q(applications__name='app name', applications_version=12)
 Computer.objects.filter(q)

 this will translate into the right where clause with both conditions.

 Now if I want the opposite, I do:
 Computer.objects.filter(~q)

 that will generate something totally different and will consider the 2
 conditions separately and generate a where clause roughly like that:

 NOT "computer"."id" IN
                         (
                                 SELECT U1."computer_id" FROM "computer" U1
 INNER JOIN "generic_application" U2 ON ( U1."application_id" = U2."id" )
 WHERE U2."name" = 'MyApp'
                         )
                         AND
                         "computer"."id" IN
                         (
                                 SELECT U1."computer_id" FROM "computer" U1
 INNER JOIN "generic_application" U2 ON ( U1."application_id" = U2."id" )
 WHERE U2."version"::text LIKE 10.0%
                         )

 So it treats the 2 conditions are totally separate and handed separately.
 Which will give me NOT all the computers that either have my app or an app
 which version starts with 10.0.

 In django 1.4, that used to work as expected (i.e. that both conditions
 would be taken into account at once.

 Now I understand that Django has to evolve I'm wonder how I can achieve
 what I want.

 BTW I purposefully simplified the example. We have a system that
 generically generates those Q object instances and moving to Django 1.6 is
 currently not really possible for us, given that behavior.

 Any help, workaround or advice is very welcome.

 Thanks.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/22709>
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].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.3005897fa5b67a3323942cd91ca1079a%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to