The Model:
class ItemType(models.Model): name = models.CharField(max_length=100) def __unicode__(self): logger.debug("1. Item Type %s created" % self.name) return self.name The code:
(...) type = re.search(r"Type:(.*?)",text) itemtype = ItemType.objects.create(name = name.group(1), defaults={'name':name.group(1)}) logger.debug("2. Item Type %s created" % name.group(1)) logger.debug("4. Item Type %s created" % itemtype.name) logger.debug("3. Item Type %s created" % itemtype) And the result is unexpected (to me of course):
The first logger.debug prints Item Type ąęńłśóć created as expected, but the second raises 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 there's an error, and how can I fix it?
(text is html response with utf-8 encoding)
updated
I add debug into model and debug result is:
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 = name.group(1), defaults={'name':name.group(1)}) if I changed it into
itemtype = ItemType.objects.create(name = name.group(1), defaults={'name':u'ĄĆĘŃŁÓŚ'}) everything was ok.
So how to convert it into unicode? unicode(name.group(1)) doesn't work.