Sorting in Django admin site on different locale

14 views
Skip to first unread message

Roland Leners

unread,
Dec 6, 2021, 10:39:58 AM12/6/21
to Django users
I am currently learning Python and Django via building a simple Web application. It involves objects with character fields that take values in different languages (French and Spanish) and I face an issue with sorting on those values in the Django administration site.

Here are sample sets:
French: dataFr = ['être', 'gronder', 'gérer', 'éviter', 'exiger']
Spanish: dataEs = ['dos', 'acompasar', 'acompañar', 'día']

The following works fine in Python:

import locale
locale.setlocale(locale.LC_ALL,"fr_FR.utf8")
sorted(dataFr, key=locale.strxfrm)
> ['être', 'éviter', 'exiger', 'gérer', 'gronder'] #which is the desired result.
sorted(dataEs, key=locale.strxfrm)
> ['acompañar', 'acompasar', 'día', 'dos'] #which is also the desired result.

(French as well as Spanish local settings seem to work indifferently for sorting  French and Spanish)

In Django, the simplified model is as follows:

from django import models

class Word(models.Model):
    content = models.CharField(max_length=32, unique=True)
    class Meta:
        abstract = True
       
class FrenchWord(Word)
        
The relevant part of the admin.py file is:

from django.contrib import admin
from .models import FrenchWord

@admin.register(FrenchWord)
class FrenchWord(admin.ModelAdmin):
    list_display = ('content',)
    ordering = ('content',)

Now my question is how to enforce ordering according to the fr_FR.utf8 locale? I have tried all kinds of stuff, taking inspiration from various sites, but I do not succeed.

Thanks in advance.
Reply all
Reply to author
Forward
0 new messages