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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/7VmYao_u1IMJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to