question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

question:UnicodeDecodeError at /admin/blog/author/import/

See original GitHub issue

Sir,thanks a lot to develop this great repository. I have little problem to solve , as below the code:

#in models.py
class Author(models.Model):
    author = models.CharField('作者', max_length=50)
    title = models.CharField('标题', max_length=150)
    qualification = models.ForeignKey(Qualification)
    mark = models.ManyToManyField(Mark)
    blog = models.TextField('博客内容')
    time = models.DateField('写作日期')

    def __unicode__(self):
        return unicode(self.author)

    class Meta:
        ordering = ['time']
#in admin.py
class AuthorAdmin(ImportExportModelAdmin, admin.ModelAdmin):
    search_fields = ('author', 'title', 'mark', 'blog')
    list_display = ('author', 'title', 'time')

When I export the data, 1 but the csv file appear to be the retortion, 2 when I modified the second row author data and import,

3 it cause the error, 4

how I it be smoothly modified and import?thanks. Allenlee

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:31 (15 by maintainers)

github_iconTop GitHub Comments

3reactions
pawelnowakcommented, Nov 16, 2015

Hi guys,

I’ve stumbled upon the exact same issue in one of my company’s projects. We’re using python3 and I’m 100% sure there’s no u’ missing on any model’s __ str__ method. Yet, the UnicodeDecodeError exception still occurs when trying to import a csv file containing utf-8 encoded characters in the django admin.

It turns out that adding the encoding param with value of utf-8 to the TempFolderStorage.open’s method seems to solve the problem, at least in our environment.

Here’s a quick’n’dirty fix in case anyone googles this issue trying to find a solution, like I did a few hours ago.

First we subclass the TempFolderStorage class, adding said param:

import tempfile
from import_export.tmp_storages import TempFolderStorage

class Utf8TempFolderStorage(TempFolderStorage):

    def open(self, mode='r'):
        if self.name:
            return open(self.get_full_path(), mode, encoding='utf-8')
        else:
            tmp_file = tempfile.NamedTemporaryFile(delete=False)
            self.name = tmp_file.name
            return tmp_file

Then we point the new class as IMPORT_EXPORT_TMP_STORAGE_CLASS in settings.py:

 IMPORT_EXPORT_TMP_STORAGE_CLASS = 'path.to.storages.Utf8TempFolderStorage'
0reactions
bmihelaccommented, Nov 20, 2015

Fixed in 0.3. please reopen if needed

Read more comments on GitHub >

github_iconTop Results From Across the Web

error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff ...
The question mark can be encoded/decoded using ASCII, UTF-8, Latin-1 and many more encodings without problems, so I don't see how it could...
Read more >
UnicodeDecodeError in for loop - GIS Stack Exchange
I solved the issue with the following code: for row in csvReader: row = [unicode (r, errors = "replace") if type (r) ==...
Read more >
'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
UnicodeDecodeError : 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte solved in Django . ... Problem Solving Point.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found