Django:sqlite文件名编码

2024-03-28 09:04:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个命令(通过manage.py importfiles)来导入我在Django中自写文件存储中的实际文件系统上的给定目录结构。在

def _handle_directory(self, directory_path, directory):
    for root, subFolders, files in os.walk(directory_path):
        for filename in files:
            path = os.path.join(root, filename)
            with open(path, 'r') as f:
                file_wrapper = FileWrapper(f)
                self.cnt_files += 1
                new_file = File(directory=directory, filename=filename,
                                file=file_wrapper, uploader=self.uploader)
                new_file.save()

full model can be found at GitHubfull command is currently on gist.github.com available。在

如果不想检查模型:我的File类的属性fileFileField。在

复制文件似乎可以工作,thanks to pajton。不过,我收到了一个新的异常,我想,sqlite编码有问题。但我不知道怎么解决它。sys.getfilesystemencoding()的值是mbcs。在

^{pr2}$

我改变了filename,但总是错的。我也尝试了'foo'或{}之类的值。:(还有.encode().decode()和{}的不同组合。在

我很确定,这是filename的问题。我打印了filename的当前值,如果文件名包含非ascii字符,则会发生异常。在

更新1:我听从pajton的建议,记录了sql查询。结果是: (第一行是打印文件名的输出)。D: \temp\prak gdv abgabe是此命令的参数。在

Eigene L÷sung.pdf
(0.000) QUERY = u'BEGIN' - PARAMS = (); args=None
(0.000) QUERY = u'INSERT INTO "filestorage_file" ("directory_id", "filename", "file", "size", "content_type", "uploader_id", "datetime", "sha512") VALUES (%s, %
s, %s, %s, %s, %s, %s, %s)' - PARAMS = (164, u'Eigene L\xf6sung.pdf', u'filestorage/5/5b/5bf32077-5531-4de0-95a7-d2ea3e10a17d.pdf', None, None, 8, u'2014-02-26
23:21:17.735000', None); args=[164, 'Eigene L\xc3\xb6sung.pdf', u'filestorage/5/5b/5bf32077-5531-4de0-95a7-d2ea3e10a17d.pdf', None, None, 8, u'2014-02-26 23:21:
17.735000', None]
(0.000) QUERY = u'BEGIN' - PARAMS = (); args=None
(0.000) QUERY = u'UPDATE "filestorage_file" SET "directory_id" = %s, "filename" = %s, "file" = %s, "size" = NULL, "content_type" = %s, "uploader_id" = %s, "date
time" = %s, "sha512" = NULL WHERE "filestorage_file"."id" = %s ' - PARAMS = (164, u'D:\\Temp\\prak-gdv-abgabe\\Protokoll\\Eigene L\ufffdsung.pdf', u'filestorage
/5/5b/5bf32077-5531-4de0-95a7-d2ea3e10a17d.pdf', u'application/pdf', 8, u'2014-02-26 23:21:17.735000', 156); args=(164, 'D:\\Temp\\prak-gdv-abgabe\\Protokoll\\E
igene L\xf6sung.pdf', u'filestorage/5/5b/5bf32077-5531-4de0-95a7-d2ea3e10a17d.pdf', 'application/pdf', 8, u'2014-02-26 23:21:17.735000', 156)

更新2:(2014-02-27 11:10 UTC) 我的sqlite数据库的编码是UTF-8,由PRAGMA encoding;验证。在

我查了数据库的记录。在

   Id   |   filename                                        |   sha512      |   size
    1   |   D:\Temp\prak-gdv-abgabe\Liesmich.html           |   ffeb8c3d5   |   5927
    2   |   D:\Temp\prak-gdv-abgabe\Liesmich.md             |   d206d241f   |   407
    3   |   D:\Temp\prak-gdv-abgabe\Liesmich.txt            |   d206d241f   |   407
    4   |   D:\Temp\prak-gdv-abgabe\Linux\GDV_Praktikum.bin |   5fc5749ee   |   166925
    5   |   Eigene Lösung.pdf                               |               |

非常有趣的是,失败的条目(id 5)具有预期的文件名,但没有设置sha512或size值。其他条目具有sha512和size的预期值,但没有预期的文件名。这很有趣。看来,custom save()-method of my File class是我问题的一部分。。。。但我不明白为什么会发生这些奇怪的事情。在


Tags: pathnoneidsizepdffilenametempdirectory
1条回答
网友
1楼 · 发布于 2024-03-28 09:04:31

嗯,我找到了一个。。。。解决方案。我刚刚改进了我的.save()模型的.save()-方法。它不再发射3+豁免而是一次。而且-这是一个重要的改变-它只更新我在自定义保存方法中检查的三个字段。我的保存方法现在看起来像:

def save(self, *args, **kwargs):
    super(File, self).save(*args, **kwargs)
    do_update = False
    if not self.content_type:
        self.content_type = mimetypes.guess_type(self.file.name)[0]
        do_update = True
    if not self.sha512:
        self.sha512 = hashlib.sha512(self.file.read()).hexdigest()
        do_update = True
    if not self.size:
        self.size = self.file.size
        do_update = True

    if do_update:
        super(File, self).save(update_fields=['content_type', 'sha512', 'size'], *args, **kwargs)

现在文件按预期导入!在

相关问题 更多 >