【问题标题】:Data migrations of files object in Django - create a file data without having the fileDjango中文件对象的数据迁移-在没有文件的情况下创建文件数据
【发布时间】:2019-10-29 03:11:46
【问题描述】:

我在迁移包含 FieldFile 对象的表时遇到问题。这里的问题是我还没有文件。我使用一种特殊的方法来创建ObjectFile

def create_object_file(file):
    return = ObjectFile.objects.create(
            name=name,
            content_type=file.content_type,
            extension=extension,
            uploaded_by=object_user,
            md5sum=file.md5sum,
            file=''
        )

文件是一个字段文件。由于我还没有文件,所以我需要在没有文件的情况下注册它。

我尝试从django.db.models 导入一个FieldFile,考虑将其实例化以便将其归因于file。但它现在错过了chunks

【问题讨论】:

    标签: python django data-migration


    【解决方案1】:

    我认为一种解决方案是使用原始 sql 来填充新数据库。

    所以,这个解决方案并不是真正解决问题:如何迁移没有文件的数据文件?

    我设法处理了一些我必须处理的文件,并且我做了我必须做的事情,这很简单:

    from django.core.files import File
    
    
    def create_source_file(file, object_file):
        media = os.path.join(SOURCE_FILES, file.path)
        if not os.path.exists(media):
            return
    
        of = ObjectFile(
            name=name,
            content_type=file.file_type,
            extension=extension.lower(),
            md5sum=file.md5sum,
        )
        with open(media, 'rb') as m:
            of.file.save(file.name.lower(), File(m))
            of.save()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2013-03-02
      • 2016-04-27
      相关资源
      最近更新 更多