views:

108

answers:

2

I need order a Queryset by date in desc order, but i need put in the end the objects at the end, I do this:

qs1 = Model.objects.exclude(date=None).order_by('-date')
qs2 = Model.objects.filter(date=None).order_by('-date')

and my list is:

l = list(qs1)+list(qs2)

There is a more efficiently way for this?

A: 

So, you're looking for all the objects that have a date ... and all the objects that don't have a date?

Wouldn't this work better:

Model.objects.order_by('-date)

Anyway, here's a good place to start ... read about Django filter chaining: http://docs.djangoproject.com/en/dev/topics/db/queries/#id1.

As a side note, isn't your query canceling itself out?

>>> d = MyModel.objects.filter(date=None).exclude(date=None).order_by('-date')
>>> d
[]
>>> d.query.get_compiler('default').as_sql()
('SELECT "date" FROM "table" WHERE ("table"."date" IS NULL AND NOT ("table"."date" IS NULL)) ORDER BY "table"."date" DESC', ())

I'm probably not understanding what you're asking...

Jason Leveille
The queries do seem to cancel each other out... unless it has something to do with grouping that I'm just not seeing. Looks like @diegueus might want the objects with no date at the end of the list, and maybe .order_by('-date') puts them at the beginning?
Chris Lawlor
Chris you're right
diegueus9
+2  A: 
Model.objects.extra(select={'nodate':'ISNULL(date)'}, order_by=['nodate', '-date'])
Ignacio Vazquez-Abrams
Doesn't work in postgres
diegueus9
So then use `'date IS NULL'` instead.
Ignacio Vazquez-Abrams