Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#24741 closed Bug (invalid)

UnicodeDecodeError in gettext via migrations autodetector

Reported by: Nicola Owned by: nobody
Component: Migrations Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


If there are no migrations to apply I get this error:

Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  No migrations to apply.
Traceback (most recent call last):
  File "", line 10, in <module>
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/core/management/", line 338, in execute_from_command_line
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/core/management/", line 330, in execute
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/core/management/", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/core/management/", line 441, in execute
    output = self.handle(*args, **options)
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/core/management/commands/", line 207, in handle
    changes = autodetector.changes(graph=executor.loader.graph)
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/db/migrations/", line 43, in changes
    changes = self._detect_changes(convert_apps, graph)
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/db/migrations/", line 186, in _detect_changes
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/db/migrations/", line 850, in generate_altered_fields
    if old_field_dec != new_field_dec:
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/utils/", line 165, in __eq__
    return self.__cast() == other
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/utils/", line 153, in __cast
    return self.__text_cast()
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/utils/", line 141, in __text_cast
    return func(*self.__args, **self.__kw)
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/utils/translation/", line 84, in ugettext
    return _trans.ugettext(message)
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/utils/translation/", line 327, in ugettext
    return do_translate(message, 'ugettext')
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/utils/translation/", line 307, in do_translate
    result = getattr(translation_object, translation_function)(eol_message)
  File "/usr/lib64/python2.7/", line 220, in ugettext
    return unicode(message)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 3: ordinal not in range(128)

Change History (5)

comment:1 Changed 7 years ago by Tim Graham

Resolution: needsinfo
Status: newclosed

It's unclear if this is a bug in your project or in Django. Could you please a sample project that reproduces the error and reopen the ticket? Thanks.

comment:2 Changed 7 years ago by Tim Graham

Summary: migration errorUnicodeDecodeError in gettext via migrations autodetector

comment:3 Changed 7 years ago by Nicola

I think you can reproduce with a models like this

# -*- coding: utf-8 -*-
from django.db import models
#from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ugettext as _

                        (90,_("90 °")),
                        (180,_("180 °")),
                        (270,_("270 °")),

class TestModel(models.Model):
        rotations = models.SmallIntegerField(choices=IMAGE_ROTATION,default=0)

makemigration fails, in my project migrations was done with 1.7 version

comment:4 Changed 7 years ago by Nicola

and is solved using something like this:

                        (90,_(u"90 °")),
                        (180,_(u"180 °")),
                        (270,_(u"270 °")),

comment:5 Changed 7 years ago by Simon Charette

Resolution: needsinfoinvalid

Hi drakkan,

As the name of the ugettext and ugettext_lazy method indicate they expect a unicode parameter to be provided.

When you call ugettext() with a utf-8 encoded byte string the method crash as expected and works correctly when you pass unicode strings (u prefixed strings).

I would suggest you make sure to define your translation strings as unicode by either prefixing them with the u prefix or using from __future__ import unicode_literals. If you must retrieve a translated string from a byte strings use the gettext and gettext_lazy method instead. Just keep in mind that Django expects unicode to be provided internally when dealing with localization.

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