0

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?

4
  • Did you try setting the encoding attribute on the FileField? docs.djangoproject.com/en/2.0/ref/files/file It's your read method that is failing to decode on the FileField not the TextField. Commented May 8, 2018 at 23:13
  • If you mean cvfile = models.FileField(encoding ="utf-8"), I have. It gives a TypeError: __init__() got an unexpected keyword argument 'encoding' Commented May 8, 2018 at 23:17
  • what version of django you are running. Is it compatible with python 3? Do you need to use python3 ? Commented May 9, 2018 at 0:32
  • Django 1.9 and yes I'll need python 3 Commented May 9, 2018 at 7:52

1 Answer 1

1

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')

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.