|
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(nam e,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
|