order by ... nulls [first|last]

561 views
Skip to first unread message

Thomas Guettler

unread,
Nov 26, 2008, 4:20:30 AM11/26/08
to django...@googlegroups.com
Hi,

I am missing order by nulls [first|last]

Either I am blind, or it is not possible.

Do other miss this, too?

Thomas

--
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de

Jarek Zgoda

unread,
Nov 26, 2008, 7:18:36 AM11/26/08
to django...@googlegroups.com
Wiadomość napisana w dniu 2008-11-26, o godz. 10:20, przez Thomas
Guettler:

> I am missing order by nulls [first|last]
>
> Either I am blind, or it is not possible.


I think this is database-dependent.

--
We read Knuth so you don't have to. - Tim Peters

Jarek Zgoda, R&D, Redefine
jarek...@redefine.pl

Thomas Guettler

unread,
Nov 26, 2008, 8:22:22 AM11/26/08
to django...@googlegroups.com
Jarek Zgoda schrieb:

> Wiadomość napisana w dniu 2008-11-26, o godz. 10:20, przez Thomas
> Guettler:
>
>
>> I am missing order by nulls [first|last]
>>
>> Either I am blind, or it is not possible.
>>
>
>
> I think this is database-dependent.
>
I know, but I would like to force this with django ORM.

something like this:
MyModel.objects.all().order_by('due_date', nulls_last=True)

I don't know if all database backends support this. Postgres does.

Malcolm Tredinnick

unread,
Nov 26, 2008, 9:14:04 PM11/26/08
to django...@googlegroups.com

On Wed, 2008-11-26 at 14:22 +0100, Thomas Guettler wrote:
> Jarek Zgoda schrieb:
> > Wiadomość napisana w dniu 2008-11-26, o godz. 10:20, przez Thomas
> > Guettler:
> >
> >
> >> I am missing order by nulls [first|last]
> >>
> >> Either I am blind, or it is not possible.
> >>
> >
> >
> > I think this is database-dependent.
> >
> I know, but I would like to force this with django ORM.
>
> something like this:
> MyModel.objects.all().order_by('due_date', nulls_last=True)
>
> I don't know if all database backends support this. Postgres does.

I'm not really in a great hurry to implement anything like this. Django
supplies a reasonable layer over SQL, but doesn't intend to cover every
single case and this is on the edge. One could also argue that if you're
routinely sorting by columns that contain NULLs and you rely on the
NULLs being first or last, you're probably not following "best
practices" at some level (and, yes, I fully realise there are
counter-arguments as well). But I think we can manage to go on living
without this type of option.

By design, QuerySets and Query are subclassable. If you really need this
support, add an extra call to your QuerySet subclass that incorporates
that into the extension of Query.as_sql() and use that QuerySet subclass
as the one returned by your custom manager.

Regards,
Malcolm

Reply all
Reply to author
Forward
0 new messages