DjangoUnicodeDecodeError: [Bad Unicode data]


class ItemType(models.Model):
  name = models.CharField(max_length=100)
  def __unicode__(self):
    logger.debug("1. Item Type %s created" %



    type ="Type:(.*?)",text)
    itemtype = ItemType.objects.create(name =, defaults={'name'})
    logger.debug("2. Item Type %s created" %
    logger.debug("4. Item Type %s created" %
    logger.debug("3. Item Type %s created" % itemtype)


And the result is unexpected (for me, of course):

The first logger.debug

prints Item Type ąęńłśóć created

as expected, but the second throws an error:

DjangoUnicodeDecodeError: 'ascii' codec can't decode byte  in position : 
ordinal not in range(128). 
You passed in <ItemType: [Bad Unicode data]> (<class 'aaa.models.ItemType'>)


Why is the error occurring and how can I fix it?

(text is html response using utf-8 encoding)


I add debug to the model and the debug output:

2014-10-06 09:38:53,342 DEBUG views 2. Item Type ąęćńółśż created
2014-10-06 09:38:53,342 DEBUG views 4. Item Type ąęćńółśż created
2014-10-06 09:38:53,344 DEBUG models 1. Item Type ąęćńółśż created
2014-10-06 09:38:53,358 DEBUG models 1. Item Type ąęćńółśż created


so why debug 3. can't print it?

UPDATE 2 The problem is here:

  itemtype = ItemType.objects.create(name =, defaults={'name'})


if i change it to

  itemtype = ItemType.objects.create(name =, defaults={'name':u'ĄĆĘŃŁÓŚ'})


everything is good.

So how do I convert it to unicode? unicode ( (1)) doesn't work.


source to share

1 answer

After 2 days of breeding with my own shadow, I found a solution. This is not a workaround for this case, but a tricky change of mind, and I have to refactor all the code.

  • My guess is EVERY LINE is UNICODE. If not, fix it.

  • do not use "% s" or "something" ALWAYS use u "% s" and u "cośtam"

  • In every model that has .CharField () models or other "text" oriented fields, I override the save () method:

in the example:

class ItemType(models.Model):
  name = models.CharField(max_length=100)

  def save(self, *args, **kwargs):
    if isinstance(, str):"utf-8")
    super(ItemType, self).save(*args, **kwargs)


Explanation - if somehow the name is filled with str not unicode - CHANGE it to unicode.

How I found this:

I was wondering what type is the text in models.CharField and found that if you fill it with unicode it is unicode, if you fill in - str - it str. Therefore, if you once filled it with "hand" using unicode, and elsewhere the regular expression filled its str, the result is unexpected.

The biggest problem with unicode and str is that there is no problem using dialect with both:

>>> text_str = "żółć"
>>> text_unicode = u"żółć"
>>> print text_str
>>> print text_uni


so you can't see the difference.

But if you use a different command:

>>> text_str
>>> text_uni


Glare difference.

if there is some tweak to change the printing behavior (and similiars) to this:

>>> print text_str
>>> print text_uni


it would be much easier to debug - if you can see the diacritics that's ok - if not - that's bad.

Using decoding ('utf-8') leads me to a solution:

>>> text_str
>>> text_str.decode('utf-8')
>>> text_uni





All Articles