Django中PostgreSQL复合类型中的文件引发“无法适应类型'TemporaryUploadedFile'”

时间:2019-09-17 06:02:50

标签: python django postgresql

我要保存文件,该文件位于PostgreSQL用户定义的复合字段中。保存不在自定义字段中的文件时,在save_form_data()中,setd属性变为FieldFile,这可能也是我想在复合字段中发生的情况。但是,当保存复合字段时,出现错误“无法适应类型'TemporaryUploadedFile'”,因为表示文件的元组元素保持TemporaryUploadedFile。如何在复合字段中保存文件?

这是我的合成字段(models.py):

class MyType(models.Field):
    name_in_field = models.CharField(max_length=100)
    file_in_field = models.FileField(
        upload_to=file_path,
        storage=OverwriteStorage()
    )

    def db_type(self, connection):
        return "custom_field"

    def save_form_data(self, instance, data):
        # I want attribute to become tuple (CharField, FieldFile)
        # just like it happens with the file outside of MyType
        # but attribute becomes (CharField, TemporaryUploadedFile)
        # what is identical to data and throws error when saving
        # Default behavior:
        setattr(instance, self.name, data)
        # I suppose I should write something like below, but what exactly?
        # setattr(getattr(instance, self.name), "name_in_field", data[0])
        # setattr(getattr(instance, self.name), "file_in_field", data[1])

我已经准备好repo进行测试。

0 个答案:

没有答案