Django countries order by translated name

862 views Asked by At

I started using django_countries and added a field to one of my models

country = CountryField(blank=True)

The problem is the users language is spanish and when the form shows the list of countries they are correctly translated, but they ordered by the code I guess, or by the English name.

I want it to be ordered by the display name so the users can easily search any country by typing the first letter and then going down until the find it (the way most of people does).

Any ideas? Thank uou for your time

2

There are 2 answers

0
steven2308 On BEST ANSWER

This was the solution, based on this

function sort(a, b) {               
        return (a.innerHTML > b.innerHTML) ? 1 : -1;
    };
$('#select_id option').sort(sort).appendTo('#select_id');
0
Bart Jellema On

If you want to do it server side, you can do something like this:

things = sorted(things, key=lambda thing:Country(thing['country']).name)

That's what I used.