1
votes

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
1

1 Answers

0
votes

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