Hi,
I have the following custom field:
from django.utils.translation import ugettext_lazy as _
COUNTRIES = (
('GB', _('United Kingdom')),
('AF', _('Afghanistan')),
('AX', _('Aland Islands')),
('AL', _('Albania')),
('DZ', _('Algeria')),
.
.
.
)
class CountryField(models.CharField):
def __init__(self, *args, **kwargs):
kwargs.setdefault('max_length', 2)
kwargs.setdefault('choices', COUNTRIES)
super(CountryField, self).__init__(*args, **kwargs)
def get_internal_type(self):
return "CharField"
That field ends up in an HTML <select>, and I'd like the countries to be sorted by their label in all locales. I have tried to replace this code:
kwargs.setdefault('choices', COUNTRIES)
with this:
kwargs.setdefault('choices', sorted(COUNTRIES, key=lambda x: x[1]))
but that's sorting the list only by the original English names, whatever the locale.
I can't switch to ugettext instead of the _lazy version, because this is a Field so it won't work at all.
Do you have any suggestion on how to get my list of countries properly sorted in all locales?
Thank you!
Salvatore Iovene.