在南部移民的情况下,FileField不允许上传到

2024-04-16 10:55:34 发布

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

我最近更改了FileField的upload \to参数,现在我正在尝试编写一个South datamigration来将存储在旧系统下的文件移动到新系统。我编写了一些FileField文档所指出的代码:

def forwards(self, orm):
    for mf in orm.ManagedFile.objects.all():
        print mf.content.path
        oldpath = mf.content.path
        cf = ContentFile(mf.content.read())
        cf.name = oldpath
        mf.content = cf
        mf.save()

这会根据一些默认规则保存所有文件,它们最终都会在MEDIA\u ROOT中释放,而不是upload\u to函数指定的位置。你知道吗

经过一番思考,我明白了为什么会这样,但我能做些什么呢?你知道吗


Tags: 文件topath文档参数系统ormcontent
1条回答
网友
1楼 · 发布于 2024-04-16 10:55:34

可以手动重新连接上载单元以执行以下操作:

 mf._meta.get_field('content').generate_filename = path_maker

生成的代码如下所示:

def path_maker(m_file, filename):
    ext = str(os.path.splitext(filename)[1])
    return os.path.join('m_files', m_file.hash) + ext

...

def forwards(self, orm):
    for mf in orm.ManagedFile.objects.all():
        mf._meta.get_field('content').generate_filename = path_maker
        print mf.content.path
        oldpath = mf.content.path
        cf = ContentFile(mf.content.read())
        cf.name = oldpath
        mf.content = cf
        mf.save()

相关问题 更多 >