1

I have a simple Django app with a Postgresql database. The database is configured with nn_NO.UTF-8 locale.

mything=>\l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 mything   | postgres | UTF8     | nn_NO.UTF-8 | nn_NO.UTF-8 | =Tc/postgres         +
           |          |          |             |             | postgres=CTc/postgres+
           |          |          |             |             | me=CTc/postgres

Let's say there's a table MyTable. I want MyTable.objects.order_by('name') to sort according to en_US rules, not nn_NO. Is it possible to override the sorting locale from Python/Django, or do I have to recreate the entire database?

settings.py contains

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
Åsmund
  • 1,332
  • 1
  • 15
  • 26
  • Check this: http://stackoverflow.com/questions/18935712/queryset-sorting-specifying-column-collation-for-django-orm-query – Smooth Fire Aug 25 '16 at 09:00

1 Answers1

0

Radosław Ganczarek's comment points to the right answer:

from django.db.models import Func, F
name_en = Func(
    'name',
    function='en_US',
    template='(%(expressions)s) COLLATE "%(function)s"')
sorted_things = MyTable.objects.order_by(name_en)

https://docs.djangoproject.com/en/1.9/ref/models/expressions/#func-expressions

Åsmund
  • 1,332
  • 1
  • 15
  • 26