How to set default ordering to Lower case?

31 views
Skip to first unread message

Neto

unread,
Jun 6, 2016, 11:21:32 PM6/6/16
to Django users
It does not works:

from django.db.models.functions import Lower


class Person(models.Model):

name = models.CharField(_('name'), max_length=100)

class Meta:
ordering = [Lower('name')]

Ezequiel Bertti

unread,
Jun 7, 2016, 1:54:46 AM6/7/16
to django...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/07398d69-2af5-4dad-9bf9-0eb32248e65f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Simon Charette

unread,
Jun 7, 2016, 4:21:09 AM6/7/16
to Django users
Hi Neto,

Ordering by expression (order_by(Lower('name')) is not supported yet but it's
tracked as a feature request[1].

If we manage to allow transforms in `order_by()`[2] in the future you should be
able to define a ['name__lower'] ordering in the future but until then you
have to use a combination of `annotate()` and `order_by()` on a custom manager:

class PersonManager(models.Manager):
    def get_queryset(self, *args, **kwargs):
        queryset = super(PersonManager, self).get_queryset(*args, **kwargs)
        return queryset.annotate(
            name_lower=Lower('name'),
        ).order_by('name_lower')


class Person(models.Model):
    name = models.CharField(_('name'), max_length=100)

Neto

unread,
Jun 7, 2016, 5:26:10 PM6/7/16
to Django users
Thanks
Reply all
Reply to author
Forward
0 new messages