Friday, July 29, 2011

Django filter and Q

Hi, i read from django docs that we can replace multiple filters with
Q. But and i used both queries but unfortunately they give me
different results. Can someone explain me this? I will post the
queries and corresponding sql here. I am filtering for a many-to-many
field.

>>> Web.objects.filter(colors=c[0]).filter(colors=c[1])
[<Web: http://www.india.com/>, <Web: http://www.saalim.com/>]

>>> Web.objects.filter(Q(colors=c[0]),Q(colors=c[1]))
[ ]

>>> str(Web.objects.filter(colors=c[0]).filter(colors=c[1]).query)
'SELECT "cart_web"."id", "cart_web"."webimage", "cart_web"."webname",
"cart_web"."weburl", "cart_web"."created_date", "cart_web"."active",
"cart_web"."category_id", "cart_web"."dev_id", "cart_web"."votes" FROM
"cart_web" INNER JOIN "cart_web_colors" ON ("cart_web"."id" =
"cart_web_colors"."web_id") INNER JOIN "cart_web_colors" T4 ON
("cart_web"."id" = T4."web_id") WHERE
("cart_web_colors"."colorpalette_id" = 1 AND T4."colorpalette_id" =
2 ) ORDER BY "cart_web"."created_date" DESC'


>>> str(Web.objects.filter(Q(colors=c[0]),Q(colors=c[1])).query)
'SELECT "cart_web"."id", "cart_web"."webimage", "cart_web"."webname",
"cart_web"."weburl", "cart_web"."created_date", "cart_web"."active",
"cart_web"."category_id", "cart_web"."dev_id", "cart_web"."votes" FROM
"cart_web" INNER JOIN "cart_web_colors" ON ("cart_web"."id" =
"cart_web_colors"."web_id") WHERE ("cart_web_colors"."colorpalette_id"
= 1 AND "cart_web_colors"."colorpalette_id" = 2 ) ORDER BY
"cart_web"."created_date" DESC'

Thank you

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment