Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#3974 closed (invalid)

UnicodeEncodeError for models with accentued __str__ (newforms-admin)

Reported by: Baptiste <baptiste.goupil@…> 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


A model with a custom _ _str_ _ method :

	def __str__(self):

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/" in get_response
  77. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/" in root
  123. return self.model_page(request, *url.split('/', 2))
File "/usr/lib/python2.4/site-packages/django/contrib/admin/" in model_page
  140. return admin_obj(request, rest_of_url)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/" in __call__
  138. return self.add_view(request)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/" in add_view
  403. return self.save_add(request, model, form, '../%s/')
File "/usr/lib/python2.4/site-packages/django/contrib/admin/" in save_add
  306. LogEntry.objects.log_action(, 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 Changed 12 years ago by Adrian Holovaty

Version: new-adminSVN

comment:2 Changed 12 years ago by miguel.filho@…

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.

comment:3 Changed 12 years ago by Malcolm Tredinnick

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 Changed 12 years ago by Baptiste <baptiste.goupil@…>

Version: SVNnewforms-admin

comment:5 Changed 12 years ago by Malcolm Tredinnick

Resolution: invalid
Status: newclosed

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 Changed 12 years ago by Baptiste <baptiste.goupil@…>

I have not tried again since the unicode merge, now it works like a charm.

Note: See TracTickets for help on using tickets.
Back to Top