Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
How do I sort choices by their localized label?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
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:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Salvatore Iovene  
View profile  
 More options Feb 11, 8:12 am
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.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kevin Harvey  
View profile  
 More options Feb 11, 8:23 am
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?

Have you tried sorting them at the template level? A filter like dictsort
might do the trick
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=ol...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ajohnston  
View profile  
 More options Feb 11, 11:59 am
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.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ajohnston  
View profile  
 More options Feb 11, 12:32 pm
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.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Salvatore Iovene  
View profile  
 More options Feb 11, 2:20 pm
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.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ajohnston  
View profile  
 More options Feb 11, 3:52 pm
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.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »