Hello everyone
I've found the following bug with newforms: when one uses form_for_model/form_for_instance methods and then does save() and form contains any non-latin (national) characters, UnicodeEncodeError? is raised.
Example (console encoding = ru_RU.UTF-8)
>>> from myproject.models import Payment
>>> from django import newforms as forms
Let's try form_for_model:
>>> PaymentForm1 = forms.models.form_for_model(Payment)
>>> form1 = PaymentForm1({'description': 'превед now', 'event_date': '2007-01-26', 'user': '1', 'pay_type': 'cash', 'amount':'50.3'})
>>> form1.is_valid()
True
>>> form1.save()
Traceback (most recent call last):
File "<console>", line 1, in ?
File "/usr/lib/python2.4/site-packages/django/newforms/models.py", line 25, in model_save
obj.save()
File "/usr/lib/python2.4/site-packages/netangels/models/payment.py", line 26, in save
super(Payment, self).save()
File "/usr/lib/python2.4/site-packages/django/db/models/base.py", line 204, in save
','.join(placeholders)), db_values)
File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 148, in execute
query = query % db.literal(args)
File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 232, in literal
return self.escape(o, self.encoders)
File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 179, in unicode_literal
return db.literal(u.encode(unicode_literal.charset))
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-5: ordinal not in range(256)
now let's try form_for_instance
>>> payment = Payment.objects.get(pk=2)
>>> PaymentForm2 = forms.models.form_for_instance(payment)
>>> form2 = PaymentForm2({'description': 'превед now', 'event_date': '2007-01-26', 'user': '1', 'pay_type': 'cash', 'amount':'50.3'})
>>> form2.is_valid()
True
>>> form2.save()
Traceback (most recent call last):
File "<console>", line 1, in ?
File "/usr/lib/python2.4/site-packages/django/newforms/models.py", line 52, in save
return save_instance(self, instance, commit)
File "/usr/lib/python2.4/site-packages/django/newforms/models.py", line 46, in save_instance
instance.save()
File "/usr/lib/python2.4/site-packages/netangels/models/payment.py", line 26, in save
super(Payment, self).save()
File "/usr/lib/python2.4/site-packages/django/db/models/base.py", line 184, in save
db_values + [pk_val])
File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 148, in execute
query = query % db.literal(args)
File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 232, in literal
return self.escape(o, self.encoders)
File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 179, in unicode_literal
return db.literal(u.encode(unicode_literal.charset))
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-5: ordinal not in range(256)
and one more little example:
>>> form2.clean_data['description'] = 'превед in unicode'
>>> form2.save()
it works because description is of type 'str', but contains Unicode characters.