On Wed, Apr 13, 2011 at 3:43 PM, Brian Bouterse <bmbou...@gmail.com> wrote:
> Could you include the output to highlight the differences?

I've just created a simpler test case. I've a couple models like:

class Parent(models.Model):
    name = models.CharField(max_length=200, db_index=True)

class Child(models.Model):
    parent = models.ForeignKey('Parent')
    gender = models.CharField(max_length=1)

Now let's filter them (I'm doing this in the ipython shell):

In [11]: f1 = Parent.objects.filter(child__gender='f')
In [12]: print f1.query
SELECT "molserv_parent"."id", "molserv_parent"."name" FROM
"molserv_parent" INNER JOIN "molserv_child" ON ("molserv_parent"."id"
= "molserv_child"."parent_id") WHERE "molserv_child"."gender" = f

Now the same with a Q object:

In [13]: q = Q(child__gender='f')
In [14]: f2 = Parent.objects.filter(q)
In [15]: print f2.query
SELECT "molserv_parent"."id", "molserv_parent"."name" FROM
"molserv_parent" INNER JOIN "molserv_child" ON ("molserv_parent"."id"
= "molserv_child"."parent_id") WHERE "molserv_child"."gender" = f

So far, so good, they are the same. Now I'll replace filter with exclude:

In [16]: f1 = Parent.objects.exclude(child__gender='f')
In [17]: print f1.query
SELECT "molserv_parent"."id", "molserv_parent"."name" FROM
"molserv_parent" WHERE NOT ("molserv_parent"."id" IN (SELECT
U1."parent_id" FROM "molserv_child" U1 WHERE (U1."gender" = f  AND
U1."parent_id" IS NOT NULL)))

In [18]: f2 = Parent.objects.exclude(q)
In [19]: print f2.query
SELECT "molserv_parent"."id", "molserv_parent"."name" FROM
"molserv_parent" INNER JOIN "molserv_child" ON ("molserv_parent"."id"
= "molserv_child"."parent_id") WHERE NOT ("molserv_child"."gender" = f
)

So only the second (Q based) filter in this case works for me. For the
records, this is with Django 1.2.5 in Fedora 14

-- 
Gianluca Sforna

http://morefedora.blogspot.com
http://identi.ca/giallu - http://twitter.com/giallu

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to