Creating an empty queryset

3,290 views
Skip to first unread message

medhat

unread,
Dec 27, 2006, 9:40:49 AM12/27/06
to Django users
Hi,

Is there a way to create an empty queryset?! Let's say I have a manager
to return all the items with the given tags, and I want to return "no
items" if one of the given tags does not exist. Right now I return an
empty list, but this causes an error in generic views. Is there a way
to create an empty QuerySet object without hitting the database?

--
Thanks,
Medhat

Adrian Holovaty

unread,
Dec 27, 2006, 1:42:11 PM12/27/06
to django...@googlegroups.com

I just had to do this the other day, and I ended up doing a hack like this:

MyModel.objects.extra(where=['1=0'])

That still hits the database, though. I'm not aware of a way to get an
empty QuerySet without hitting the database. Maybe we should add one?

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com

Jeremy Dunck

unread,
Dec 27, 2006, 2:00:46 PM12/27/06
to django...@googlegroups.com
On 12/27/06, medhat <medhat...@gmail.com> wrote:
...

>Right now I return an
> empty list, but this causes an error in generic views.

I think you want allow_empty=True.

Adam Seering

unread,
Jan 10, 2007, 3:39:49 AM1/10/07
to django...@googlegroups.com
I would strongly second that. This seems to have fallen somewhat
dead, though. Any thoughts?; anyone in favor of it?; anyone know of
any reasons not to do it?

Adam

Michael Radziej

unread,
Jan 10, 2007, 4:58:05 AM1/10/07
to django...@googlegroups.com
Adam Seering schrieb:

> I would strongly second that. This seems to have fallen somewhat
> dead, though. Any thoughts?; anyone in favor of it?; anyone know of
> any reasons not to do it?

+1 from me. I have already missed it several times. The "where
1=0 solution" looks stupid. And the changed implementation of
QuerySet.in() also needs this (and uses where 1=0).


Michael

--
noris network AG - Deutschherrnstraße 15-19 - D-90429 Nürnberg -
Tel +49-911-9352-0 - Fax +49-911-9352-100

http://www.noris.de - The IT-Outsourcing Company

Russell Keith-Magee

unread,
Jan 10, 2007, 8:41:03 PM1/10/07
to django...@googlegroups.com
On 1/10/07, Michael Radziej <m...@noris.de> wrote:
>
> Adam Seering schrieb:
> > I would strongly second that. This seems to have fallen somewhat
> > dead, though. Any thoughts?; anyone in favor of it?; anyone know of
> > any reasons not to do it?
>
> +1 from me. I have already missed it several times. The "where
> 1=0 solution" looks stupid. And the changed implementation of
> QuerySet.in() also needs this (and uses where 1=0).

I'm +1 on adding a function for adding an empty queryset. Now, if only
a patch would materialize out of the ether... :-)

As for the '1=0' being stupid (and in() requiring it); agreed. 'where
in ()' and 'where false' don't work on all DB backends, and it's
non-trivial to optimize the logic chain to remove the need for the
dumb clause.

Any suggestions on better ways to represent this are greatfully
accepted. Patches are even more greatfully accepted.

Yours,
Russ Magee %-)

medhat

unread,
Jan 11, 2007, 6:32:25 AM1/11/07
to Django users
The patch is in http://code.djangoproject.com/ticket/3283

--
Thanks,
Medhat

Russell Keith-Magee

unread,
Jan 11, 2007, 6:57:47 AM1/11/07
to django...@googlegroups.com
On 1/11/07, medhat <medhat...@gmail.com> wrote:
>
> The patch is in http://code.djangoproject.com/ticket/3283

Remember - no patch is complete without unit tests and documentation...

Yours,
Russ Magee %-)

medhat

unread,
Jan 11, 2007, 11:00:14 AM1/11/07
to Django users
Well, the patch now has documentation and tests.

--
Thanks,
Medhat

Reply all
Reply to author
Forward
0 new messages