2

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

karthikr
  • 97,368
  • 26
  • 197
  • 188
steven2308
  • 2,250
  • 1
  • 19
  • 22
  • Translate names in view, sort it there and then push to the template. What do you use for translations? – freakish Aug 20 '13 at 18:21
  • Actually I'm not doing anything to translate so that answer won't work, I guess the django_countries app does it automatically since I have spanish as language in my settings.py – steven2308 Aug 21 '13 at 03:13
  • That's interesting. Unfortunetly I don't know that app. But you can always try to sort these countries on client side (JavaScript?). – freakish Aug 21 '13 at 06:17
  • Apparently that's gonna be the only solution :/ Thank you – steven2308 Aug 22 '13 at 22:07
  • http://stackoverflow.com/questions/25626044/django-countries-wrong-sorting-in-translated-choices-but-works-in-the-admin – Wtower May 06 '15 at 09:56

2 Answers2

1

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');
Community
  • 1
  • 1
steven2308
  • 2,250
  • 1
  • 19
  • 22
-1

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.