Skip to content

Instantly share code, notes, and snippets.

@domibarton
Last active March 4, 2017 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domibarton/aa691342321f63f3511bb1ff53b946c7 to your computer and use it in GitHub Desktop.
Save domibarton/aa691342321f63f3511bb1ff53b946c7 to your computer and use it in GitHub Desktop.
Django & MySQL & custom unicode class = FAIL
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from django.views.generic import View
from django.http import HttpResponse
from .models import MyFirstModel, MySecondModel
class CustomUnicode(unicode):
pass
class TestView(View):
def get(self, request, attr, *args, **kwargs):
spam = Spam.objects.get(id=1)
# This works
hello = 'Hellö'
spam.a_text_field = hello
spam.save()
# This also works.
hello = unicode('Hellö')
spam.a_text_field = hello
spam.save()
# This will throw an UnicodeError!
hello = CustomUnicode('Hellö')
spam.a_text_field = hello
spam.save()
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "/media/psf/Public/meh/app/views.py" in get
56. spam.save()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save
796. force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save_base
824. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in _save_table
889. forced_update)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in _do_update
939. return filtered._update(values) > 0
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in _update
654. return query.get_compiler(self.db).execute_sql(CURSOR)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py" in execute_sql
1148. cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py" in execute_sql
835. cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py" in execute
79. return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py" in execute
110. return self.cursor.execute(query, args)
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/cursors.py" in execute
187. query = query % tuple([db.literal(item) for item in args])
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py" in literal
278. return self.escape(o, self.encoders)
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py" in string_literal
203. return db.string_literal(obj)
Exception Type: UnicodeEncodeError at /app/test/value1/
Exception Value: 'ascii' codec can't encode character u'\xe4' in position 1: ordinal not in range(128)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment