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
1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.