#3974 closed (invalid)
UnicodeEncodeError for models with accentued __str__ (newforms-admin)
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
Component: | contrib.admin | Version: | newforms-admin |
Severity: | Keywords: | newforms-admin | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
A model with a custom _ _str_ _ method :
def __str__(self): return self.name
If you try to add an item with an accent in the field name:
Traceback (most recent call last): File "/usr/lib/python2.4/site-packages/django/core/handlers/base.py" in get_response 77. response = callback(request, *callback_args, **callback_kwargs) File "/usr/lib/python2.4/site-packages/django/contrib/admin/sites.py" in root 123. return self.model_page(request, *url.split('/', 2)) File "/usr/lib/python2.4/site-packages/django/contrib/admin/sites.py" in model_page 140. return admin_obj(request, rest_of_url) File "/usr/lib/python2.4/site-packages/django/contrib/admin/options.py" in __call__ 138. return self.add_view(request) File "/usr/lib/python2.4/site-packages/django/contrib/admin/options.py" in add_view 403. return self.save_add(request, model, form, '../%s/') File "/usr/lib/python2.4/site-packages/django/contrib/admin/options.py" in save_add 306. LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(model).id, pk_value, str(new_object), ADDITION) UnicodeEncodeError at /admin/model/model/add/ 'ascii' codec can't encode character u'\xe0' in position 9: ordinal not in range(128)
But the item is added to the database, and can be seen without problem in the admin site after that.If we want to edit it, the validation of the form raises the same error, but again, changes are correctly saved.
Change History (6)
comment:1 by , 17 years ago
Version: | new-admin → SVN |
---|
comment:2 by , 17 years ago
comment:3 by , 17 years ago
The previous comment doesn't seem relevant to this ticket. This ticket is about newforms-admin (which has not yet been ported to use Unicode). Your comment seems to be about trunk. If that is the case, please open a new ticket and provide a short example of how to repeat the problem.
comment:4 by , 17 years ago
Version: | SVN → newforms-admin |
---|
comment:5 by , 17 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
This is not a bug in Django.
If you are expecting to handle non-ACSII data you are responsible for converting the output to UTF-8 in your models' __str__
methods. Alternatively (and this is the best approach), create a __unicode__
method instead of a __str__
method and Django will take care of providing a default __str__
that calls __unicode__
and converts it to UTF-8. This is all documented in docs/unicode.txt.
comment:6 by , 17 years ago
I have not tried again since the unicode merge, now it works like a charm.
I have updated my django, using the current SVN with the unicode branch merged, and I started getting this same problem.
I have fixed it replacing _ _str_ _ with _ _unicode_ _, as recommended in the recent documentation for the unicode support.
http://code.djangoproject.com/wiki/UnicodeBranch#PortingApplicationsTheQuickChecklist