Django-NYC Message Board Django-NYC Discussion Forum › App Engine/Django RadioSelect Choices Internationalization

App Engine/Django RadioSelect Choices Internationalization

Robert Hancock
Posted Apr 7, 2010 2:55 PM
rhancock
New York, NY
Post #: 6
Send an Email Post a Greeting
You probably won't come across this much, but if you internationalize your sites:

To make GAE django accept international (tested only with Chinese
traditional) RadioSelect Choices:

Method 1. get Google to change
field.MultipleChoiceField.clean
from this:
valid_values = set([str(k) for k, v in self.choices])
to this:
valid_values = set([smart_unicode(k) for k, v in self.choices])

Method 2. Use code like this

class IntRadioSelect(forms.RadioSelect):
def render(self, name, value, attrs=None, choices=()):
renderer
=super(IntRadioSelect,self).render(name,value,attrs,choices)
return renderer.__unicode__()

from django.conf import settings

def smart_unicode_from_util(s): # <- use this function to avoid 'no
module named util' error
if not isinstance(s, basestring):
if hasattr(s, '__unicode__'):
s = unicode(s)
else:
s = unicode(str(s), settings.DEFAULT_CHARSET)
elif not isinstance(s, unicode):
s = unicode(s, settings.DEFAULT_CHARSET)
return s


def IntChoiceFieldClean(self, value):
"""
Validates that the input is in self.choices.
"""
value = super(forms.fields.ChoiceField, self).clean(value)
EMPTY_VALUES = (None, '')
if value in EMPTY_VALUES:
value = u''
value = smart_unicode_from_util(value)
if value == u'':
return value
valid_values = set([smart_unicode_from_util(k) for k, v in
self.choices])
if value not in valid_values:
raise ValidationError(gettext(u'Select a valid choice.
That choice is not one of the available choices.'))
return value

forms.fields.ChoiceField.clean = IntChoiceFieldClean

Powered by mvnForum

Yipit

Django Speaker Series

AxialMarket

Supporters of DjangoNYC who are actively hiring software engineers

NWCU - Community-Powered Learning

Thanks to the awesome support of Google, BlackBerry & New Work City.

Other nearby
Meetups
Why these groups?
x

The Meetup Groups shown here are topically similar to Django-NYC.

Groups are more likely to be displayed here if they:

  • have a Meetup scheduled
  • have a high rating
  • have a group photo
  • are "public" and not "private"
  • have shown they are likely to stick around (older than 30 days)
Find more Meetup Groups
near Brooklyn

Log in

  • Not registered with us yet?
or

Log in to Meetup with your Facebook account.

Sign up

or

Join this Meetup Group even quicker with your Facebook account.

By clicking the "Sign up using Facebook" or "Sign up" buttons above, you agree to Meetup's Terms of Service