Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Bug when OR'ing an empty Q() with a non-empty Q()? [RSS Feed]

#1 Jan. 3, 2011 23:16:53

Margie R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Bug when OR'ing an empty Q() with a non-empty Q()?


I find that if I filter by ORing an empty Q() with a non-empty Q()
that I don't get the expected result. I would expect that this should
return all objects of the specified model that the empty Q() would
return. Let me use an example to be more clear:

I have a single Task object with its name set to foo.

Task.objects.filter(Q()) gives me that one object:


But:

Task.objects.filter(Q()|Q(name="bar"))

returns

Is this a bug or am I misunderstanding something? This is on Django
1.2.1

Thanks for any pointers,

Margie

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

Offline

#2 Jan. 6, 2011 03:14:17

Karen T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Bug when OR'ing an empty Q() with a non-empty Q()?


On Mon, Jan 3, 2011 at 6:16 PM, Margie Roginski <margierogin...@yahoo.com>wrote:

> I find that if I filter by ORing an empty Q() with a non-empty Q()
> that I don't get the expected result. I would expect that this should
> return all objects of the specified model that the empty Q() would
> return. Let me use an example to be more clear:
>
> I have a single Task object with its name set to foo.
>
> Task.objects.filter(Q()) gives me that one object:
>
>
> But:
>
> Task.objects.filter(Q()|Q(name="bar"))
>
> returns
>
> Is this a bug or am I misunderstanding something? This is on Django
> 1.2.1
>
>
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. Similarly combining it with another Q generates no SQL, so
you are left with the other Q results.

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

Offline

#3 Jan. 6, 2011 13:16:14

bruno d.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Bug when OR'ing an empty Q() with a non-empty Q()?


On 6 jan, 04:14, Karen Tracey <kmtra...@gmail.com> 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
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
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 django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Bug when OR'ing an empty Q() with a non-empty Q()? [RSS Feed]

Board footer

Moderator control

Enjoy the 11th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support