tickets: 3370
This data as json
id | created | changetime | last_pulled_from_trac | stage | status | component | type | severity | version | resolution | summary | description | owner | reporter | keywords | easy | has_patch | needs_better_patch | needs_tests | needs_docs | ui_ux |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
3370 | 2007-01-26 06:35:57 | 2011-10-09 12:44:57 | 2022-03-06 03:28:31.975157 | Accepted | closed | Forms | dev | fixed | [patch] newforms: form.save() raises UnicodeEncodeError when form contains any non latin characters | 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. | adrian | anton@khalikov.ru | newforms utf8 unicode-branch | 0 | 1 | 1 | 1 | 0 | 0 |