Monday, June 6, 2016

Re: How to set default ordering to Lower case?

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)

    objects = PersonManager()

Cheers,
Simon

[1] https://code.djangoproject.com/ticket/26257
[2] https://code.djangoproject.com/ticket/24747

Le lundi 6 juin 2016 19:21:32 UTC-4, Neto a écrit :
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')]

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/be479f89-8b5f-4fc2-ad27-2e4e17316bec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment