﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
22709	Problem with negating Q object	thierryb@…	nobody	"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."	Uncategorized	closed	Database layer (models, ORM)	1.6	Normal	duplicate			Unreviewed	0	0	0	0	0	0
