How do I sort choices by their localized label?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 |
From: Salvatore Iovene <salvatore.iov...@gmail.com>
Date: Sat, 11 Feb 2012 05:12:11 -0800 (PST)
Local: Sat, Feb 11 2012 8:12 am
Subject: How do I sort choices by their localized label?
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 must Sign in before you can post messages.
You do not have the permission required to post.
|
 |
From: Kevin Harvey <kchar...@gmail.com>
Date: Sat, 11 Feb 2012 05:23:14 -0800 (PST)
Local: Sat, Feb 11 2012 8:23 am
Subject: Re: How do I sort choices by their localized label?
You must Sign in before you can post messages.
You do not have the permission required to post.
|
 |
From: ajohnston <ajohnston...@gmail.com>
Date: Sat, 11 Feb 2012 08:59:39 -0800 (PST)
Local: Sat, Feb 11 2012 11:59 am
Subject: Re: How do I sort choices by their localized label?
I think this should work:
>>> COUNTRIES = (
... ('GB', _('United Kingdom')),
... ('AF', _('Afghanistan')),
... ('AX', _('Aland Islands')),
... ('AL', _('Albania')),
... ('DZ', _('Algeria')),)
>>> sorted(COUNTRIES, key=lambda COUNTRIES: COUNTRIES[1])
[('AF', 'Afghanistan'), ('AX', 'Aland Islands'), ('AL', 'Albania'),
('DZ', 'Algeria'), ('GB', 'United Kingdom')]
You must Sign in before you can post messages.
You do not have the permission required to post.
|
 |
From: ajohnston <ajohnston...@gmail.com>
Date: Sat, 11 Feb 2012 09:32:09 -0800 (PST)
Local: Sat, Feb 11 2012 12:32 pm
Subject: Re: How do I sort choices by their localized label?
Sorry I just realized that even though that sorts them correctly, it
doesn't solve your problem.
Have you tried setting the choices in your form, like:
class SomeFormUsingCountryField(forms.Form):
def __init__(self, *args, **kwargs):
super(SomeFormUsingCountryField, self).__init__(*args,
**kwargs)
self.fields['country_field'].choices = sorted(COUNTRIES,
key=lambda c: c[1])
You must Sign in before you can post messages.
You do not have the permission required to post.
|
 |
From: Salvatore Iovene <salvatore.iov...@gmail.com>
Date: Sat, 11 Feb 2012 11:20:29 -0800 (PST)
Local: Sat, Feb 11 2012 2:20 pm
Subject: Re: How do I sort choices by their localized label?
On Saturday, February 11, 2012 7:32:09 PM UTC+2, ajohnston wrote: > Have you tried setting the choices in your form, like: > class SomeFormUsingCountryField(forms.Form): > def __init__(self, *args, **kwargs): > super(SomeFormUsingCountryField, self).__init__(*args, > **kwargs) > self.fields['country_field'].choices = sorted(COUNTRIES, > key=lambda c: c[1])
Funny, that fixes it. Thanks! It's a layer violation, so I have to do it for each form that uses my field, but it works. Salvatore.
You must Sign in before you can post messages.
You do not have the permission required to post.
|
 |
From: ajohnston <ajohnston...@gmail.com>
Date: Sat, 11 Feb 2012 12:52:48 -0800 (PST)
Local: Sat, Feb 11 2012 3:52 pm
Subject: Re: How do I sort choices by their localized label?
> class CountryField(models.CharField):
> def __init__(self, *args, **kwargs):
> kwargs.setdefault('max_length', 2)
> kwargs.setdefault('choices', COUNTRIES)
> super(CountryField, self).__init__(*args, **kwargs)
Btw, the reason this doesn't work is because setdefault doesn't work
as you think it does. See [1] for an example.
For the 'layer violation', you might try:
class CountryField(models.CharField):
def __init__(self, *args, **kwargs):
super(CountryField, self).__init__(*args, **kwargs)
self.choices = sorted(COUNTRIES, key=lambda c: c[1])
...
I haven't tried this but it might work.
Cheers
[1]http://www.saltycrane.com/blog/2010/02/python-setdefault-example/
You must Sign in before you can post messages.
You do not have the permission required to post.
|
|
|