I have a list of keywords
keywords = [u'encendió', u'polémica']
I am trying to load them to a django model:
class myKeywords(model.Model):
keyword = models.charField()
def __unicode__(self):
return self.keyword.encode('utf-8')
This is what i am trying:
for k in keywords:
keyObj, created = myKeywords.objects.get_or_create(keyword=k.decode('utf-8'))
print created, keyObj
However, I keep getting the django.utils.encoding.DjangoUnicodeDecodeError: 'ascii' codec can't decode byte.
I have tried:
- adding/removing
ufrom infront of the keyword - removing
decode('utf-8')while creating the keyword object -- doing this successfully creates and saves the object if there is auappended infront of the keyword - removing
encode('utf-8')from the__unicode__(self)function. -- doing this successfully prints the keyword
So, the only configuration that is working is as follows:
- keep
uappended in-front of the keyword - dont do
decode('utf-8')orencode('utf-8')anyplace else
But I am not sure if this is the right way of doing this. Ideally I should be reading a keyword and decoding it as utf-8 and then be saving it to the db. Any suggestions?