views:

32

answers:

3

Hi,

I have a simple model

title = models.CharField(max_length=250)
url = models.CharField(max_length=250)
title_french = models.CharField(max_length=250)

I want to order it via title_french, however when it orders in A-Z in this way, all the blank values are at the top. In the case ot blank values I display the English title.

So I get A-Z for the French titles, but at the top there is a load of English title unordered.

Any advice?

+1  A: 

have you tried ordering by multiple fields (doc):

ordering = ('title_french', 'title')
second
A: 

specify both the columns, title_french and title in order_by

queryset.order_by('title_french', 'title')

title_french will be given first preference and if there are two entries with the same title_french then those two entries will be sorted by their title

Ashok
this will order both the English and Frech, however, since the Frech has a lot of blanks all the English come first.I want my ordering to have all the non-blank frech first, then the English, or just everything english/french A-Z
Mark
+1  A: 

For your case, I think you should do the sorting in your python code (currently, as it is, the sorting is made in the database). It is not possible, imho, to do what you want in the db, at least without writing some sql by hand.

So the idea would be to do something like this in your view :

your_objects = list(YourObject.objects.filter(....))

your_objects.sort(key=lambda ob:  ob.title_french if ob.title_french else  ob.title)

As long as you sort small lists, this should not be a too problematic performance issue.

madewulf
"its not possible in django", thats what i wanted to know, plus thank for the alternative solution - it looks neat!
Mark