Yunter
Yunter

Reputation: 284

UnicodeDecodeError adding model in Django

When trying to add a model containing unicode in Django 1.9, I get the following error:

UnicodeDecodeError at /cleaner/clean/add/
'utf-8' codec can't decode byte 0x96 in position 209: invalid start byte

This occurs in the model class.

class Clean(models.Model):
    name = models.CharField(max_length=100)
    cv = models.TextField(max_length=10000, blank = True, null = True)
    cvfile = models.FileField(validators=[validate_file_extension])

    #override save method
    def save(self, *args, **kwargs):
        get_text = self.cvfile.read()
        self.cv = get_text
        self.cv=self.cv.decode("utf-8")
        super(Clean, self).save(*args, **kwargs)

I thought self.cv.decode("utf-8") would solve this as I'm using python 3.6.4, but it does not.

Is there a way to solve this?

Upvotes: 0

Views: 118

Answers (1)

Yunter
Yunter

Reputation: 284

Got it. For anyone that might have this problem in the future:

To fix this, change the line: self.cv=self.cv.decode("utf-8")

to: get_text = self.cvfile.read().decode("utf-8", 'ignore')

Upvotes: 1

Related Questions