Malcolm Tredinnick wrote:
> On Thu, 2007-05-10 at 01:45 +0200, Thomas Rabaix wrote:
>> Hello,
>>
>> I have a newform object which I prepopulate some fields :
>>
>> form = MenuForm()
>>
>> tu = ()
>> for m in Menu.objects.all():
>> tu += ((m.id,m),)
>> form.fields['parent_id'].choices = tu
>>
>> In the case of Menu.__str__ return a utf-8 string, the SelectWidget
>> raise an UnicodeError. To make it work I have to make this changes :
>>
>> --- widgets.py (revision 5173)
>> +++ widgets.py (working copy)
>> @@ -168,7 +168,7 @@
>> for option_value, option_label in chain(self.choices, choices):
>> option_value = smart_unicode(option_value)
>> selected_html = (option_value == str_value) and u'
>> selected="selected"' or ''
>> - output.append(u'<option value="%s"%s>%s</option>' %
>> (escape(option_value), selected_html, escape(smart_unicode(option_label))))
>> + output.append(u'<option value="%s"%s>%s</option>' %
>> (escape(option_value), selected_html,
>> escape(smart_unicode(option_label.__str__()))))
>> output.append(u'</select>')
>> return u'\n'.join(output)
>>
>> is it a bug or am i doing something in the wrong way ?
>
> Sounds like you option_label is wrapped in a gettext_lazy() call, which
> is known not to work (otherwise, adding the extra __str__ wouldn't do
> anything as far as I can see).
>
> Of course, there are a lot of known problems with unicode handling in
> trunk, which is why we have the unicode branch in development, as
> mentioned here a few times.
>
> Regards,
> Malcolm
>
>
>
> >
Well, I have put back the first change in widgets.py and add the this to
the models.py in newforms
--- models.py (revision 5173)
+++ models.py (working copy)
@@ -107,6 +107,8 @@
fields = SortedDictFromList([(f.name, f.formfield()) for f in
field_list if f.editable])
return type('FormForFields', (BaseForm,), {'base_fields': fields})
+from django.utils.encoding import StrAndUnicode, smart_unicode
+
class QuerySetIterator(object):
def __init__(self, queryset, empty_label, cache_choices):
self.queryset, self.empty_label, self.cache_choices =
queryset, empty_label, cache_choices
@@ -115,7 +117,8 @@
if self.empty_label is not None:
yield (u"", self.empty_label)
for obj in self.queryset:
- yield (obj._get_pk_val(), str(obj))
+ value = smart_unicode(obj.__str__())
+ yield (obj._get_pk_val(), value)
# Clear the QuerySet cache if required.
if not self.cache_choices:
self.queryset._result_cache = None
The iterator did not return a unicode string.
And when I populated my form I just do this :
form = MenuForm()
tu = ()
for m in Menu.objects.all():
tu += ((m.id,smart_unicode(m.__str__())),)
form.fields['parent_id'].choices = tu
All seems to works fines ...
Thomas Rabaix
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
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
-~----------~----~----~----~------~----~------~--~---