On 6 jan, 04:14, Karen Tracey <[email protected]> wrote: > > Near as I can tell the doc doesn't say anything about what effect an empty > Q() is supposed to have, so I don't know if the behavior you describe is a > bug or not. > > However, the behavior you describe makes sense to me if you take the view > that and empty Q() just doesn't apply any conditions to the query, and > generates no SQL.
>>> PageInternet.objects.count() 13 >>> PageInternet.objects.filter().count() 13 >>> PageInternet.objects.filter(Q()).count() 13 >>> Obviously calling qs.filter without args yields the same results as calling qs.all - which makes sense to me - and calling qs.filter with an empty Q yields the same result as calling it without args - which also makes sense. Now: >>> PageInternet.objects.filter(id="PAG-INT.2010-12-20.015182").count() 1 >>> PageInternet.objects.filter(id="PAG-INT.2010-12-20.015182-yadda").count() 0 >>> q1 = Q(id="PAG-INT.2010-12-20.015182") | >>> Q(id="PAG-INT.2010-12-20.015182-yadda") >>> PageInternet.objects.filter(q1).count() 1 >>> connection.queries[-1]['sql'] u'SELECT COUNT(*) FROM `page_internet` INNER JOIN `content` ON (`page_internet`.`content_id` = `content`.`content_id`) WHERE (`content`.`id` = PAG-INT.2010-12-20.015182 OR `content`.`id` = PAG- INT.2010-12-20.015182-yadda )' >>> q2 = Q() | Q(id="PAG-INT.2010-12-20.015182-yadda") >>> PageInternet.objects.filter(q2).count() 0 >>> connection.queries[-1]['sql'] u'SELECT COUNT(*) FROM `page_internet` INNER JOIN `content` ON (`page_internet`.`content_id` = `content`.`content_id`) WHERE (`content`.`id` = PAG-INT.2010-12-20.015182-yadda )' >>> The empty Q doesn't generate a where clause, indeed - so from this POV it's not "a bug" if OR'ing an empty Q with a non-empty one yields the same result as using only the non-empty one. OTHO, it's rather counter-intuitive - from a logical POV - that OR'ing a condition that yields the whole set and a condition that yields a subset doesn't yields the whole set. It's just like having "True or whatever_boolean_expression" evaluating to False... As someone famous here used to say in his sig: "Bureaucrat Conrad, you are technically correct -- the best kind of correct." !-) More seriously: whether this is how it "should" work or not is a design choice, and as such belongs to the team. But whatever they decide(d), it might be worth documenting the behaviour to avoid confusing the users. My 2 cents... -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

