Opened 17 years ago

Closed 17 years ago

Last modified 17 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

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 Adrian Holovaty, 17 years ago

Version: new-adminSVN

comment:2 by miguel.filho@…, 17 years ago

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

comment:3 by Malcolm Tredinnick, 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 Baptiste <baptiste.goupil@…>, 17 years ago

Version: SVNnewforms-admin

comment:5 by Malcolm Tredinnick, 17 years ago

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

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