﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
14837	field based custom ordering	Peter of the Norse <RahmCoff+dj@…>	nobody	"I have a model of bands. The problem is, “The Beatles”, along with most of the other bands are filed under ‘T’. I want them right after “Beat Happening”. So I wrote a simple function to do that. {{{if arg[0][0:4] == 'The ': return arg[0][4:]}}} basically. PostgreSQL allows indexes on functions, so no performance problem. Until I try to use it in Django. I wrote this workaround:

{{{
SansTheManager(models.Manager):
    def get_query_set(self):
        return super(SansTheManager, self).get_query_set().\
            extra(select={'_sans_the_order': 'sans_the(name)'},\
            order_by=['_sans_the_order'])

class Band(models.Model):
    name = CharField('Band name', max_length=255, unique=True)

    objects = SansTheManager()
}}}

This works, until I try to use it in the Admin interface. Then the order goes out the window. Also, I have to use {{{order_by('other_field', '_sans_the_ordering')}}} which looks ugly. And that does happen. So I would like to be able to order by expressions somehow. My ideal solution would involve using fields to provide the ordering. That way it would work with Admin too.

My suggestion:
{{{
class Band(models.Model):
    name = CharField('Band name', max_length= 255, unique= True, ordering= 'sans_the(name)')

    class Meta:
        ordering = 'name',"		closed	Database layer (models, ORM)	dev		wontfix			Unreviewed	0	0	0	0	0	0
