简体   繁体   中英

Django admin tries to encode CharField as “ascii”

I know this has been asked a zillon times, but I read those zillon answers and nothing was useful to fix this particular issue.

This is a minimal model example of my issue:


The error was not on the CharField but while saving a related file (with the same name). I misunderstand the original error. Thank you everybody for the kind help :)


I'm using python 2.7, django 1.7 and postgres as DB backend. When I try to save the word "Resúmenes" as model "titulo", I get the nepharious

Exception Value:'ascii' codec can't encode character u'\\xfa'...

I understand django default is to use UTF-8 (and it's working like that in many other models) and I have not changed anything related. I verified DB is also using UTF-8 for the DB/tables/columns.

Any hint what may be going on?

EDIT: I'm using Django admin to create the object. The full traceback is this one:

Environment:


Request Method: POST
Request URL: http://resumenesonline.com.ar/admin/resumenes/resumen/add/

Django Version: 1.7.7
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'allauth.socialaccount.providers.facebook',
 'allauth.socialaccount.providers.google',
 'taggit',
 'resumenes')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
  583.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  105.                     response = view_func(request, *args, **kwargs)
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  52.         response = view_func(request, *args, **kwargs)
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
  206.             return view(request, *args, **kwargs)
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in add_view
  1453.         return self.changeform_view(request, None, form_url, extra_context)
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
  29.             return bound_func(*args, **kwargs)
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  105.                     response = view_func(request, *args, **kwargs)
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
  25.                 return func.__get__(self, type(self))(*args2, **kwargs2)
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/db/transaction.py" in inner
  394.                 return func(*args, **kwargs)
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in changeform_view
  1404.                 self.save_model(request, new_object, form, not add)
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in save_model
  1045.         obj.save()
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/db/models/base.py" in save
  589.                        force_update=force_update, update_fields=update_fields)
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/db/models/base.py" in save_base
  617.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/db/models/base.py" in _save_table
  698.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/db/models/base.py" in _do_insert
  731.                                using=using, raw=raw)
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/db/models/manager.py" in manager_method
  92.                 return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/db/models/query.py" in _insert
  921.         return query.get_compiler(using=using).execute_sql(return_id)
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  919.             for sql, params in self.as_sql():
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in as_sql
  877.                 for obj in self.query.objs
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/db/models/fields/files.py" in pre_save
  301.             file.save(file.name, file, save=False)
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/db/models/fields/files.py" in save
  89.         self.name = self.storage.save(name, content)
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/core/files/storage.py" in save
  50.         name = self.get_available_name(name)
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/core/files/storage.py" in get_available_name
  75.         while self.exists(name):
File "/webapps/resumenesOnline/local/lib/python2.7/site-packages/django/core/files/storage.py" in exists
  265.         return os.path.exists(self.path(name))
File "/webapps/resumenesOnline/lib/python2.7/genericpath.py" in exists
  18.         os.stat(path)

Exception Type: UnicodeEncodeError at /admin/resumenes/resumen/add/
Exception Value: 'ascii' codec can't encode character u'\xfa' in position 67: ordinal not in range(128)

Your traceback doesn't match your model definition. Traceback shows you have a FileField somewhere.

In any case, it's suggested that you check your system locale and make sure it's set to UTF-8.

See also:

This should work, add this method to your model, and take away your original Unicode method.

def __str__(self):
  return self.titulo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM