上传文件后保存更新FileField文件(Django) [英] Update FileField filename after uploaded file saved (Django)

查看:1422
本文介绍了上传文件后保存更新FileField文件(Django)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我明白了,您如何更改Django 中的媒体文件(用户上传的文件)的文件名?如果您希望在上传 时,您可以按照这个 a>,但我正在谈论更改已经在数据库中的图像的名称。



我已经尝试覆盖 ImageFileField 然后保存模型,但这不影响文件本身。它只是打破了引用,因为现在 ImageFileField 指向新的名称,但文件仍然有旧的。



使用与链接问题相同的示例模型:

  class SomeModel(models.Model):
title = models.CharField(max_length = 100)
video = models.FileField(upload_to ='video')

这不起作用:

 >>>对于m在SomeModel.objects.all()中:
... m.video.name ='new_video_name.avi'
... m.save()

我在这里做什么?

解决方案

没有魔术和简单的方法来做到这一点。从数据库(for循环)加载对象时,由视频属性引用的文件的名称也从数据库加载。您不能只是更改它,并使底层文件更改它的名称。



为了完成物理文件和数据库中的引用的名称更改,必须手动重命名物理文件。这样做应该可以工作:

  import os 

class SomeModel(models.Model):
file_field = ...

def rename(self,new_name):
old_path = self.file_field.path
self.file_field.name = new_name

os.rename(old_path,self.file_field.path)
self.save()


在SomeModel.objects.all()中:
new_name = compute_new_filename(m)
m.rename(new_name)

请注意,一个简化的版本,并且可能希望在执行 os.rename()时对可能的IO错误进行一些处理。



此代码假定您正在使用FileSystemStorage而不是其他自定义存储类。在这种情况下,实施将需要适应该特定的存储机制。


How do you change the filename of a media file (user-uploaded file) in Django after it's been saved?

I understand that if you want to do it as it's being uploaded that you can follow the solution in questions like this, but I'm talking about changing the names of images that are already in the database.

I have tried overriding the name attribute of the ImageFileField and then saving the model, but this does not affect the file itself. It just breaks the reference because now the ImageFileField is pointing at the new name but the file still has the old one.

Using the same example model as the linked question:

class SomeModel(models.Model):
    title = models.CharField(max_length=100)
    video = models.FileField(upload_to='video')

This does NOT work:

>>> for m in SomeModel.objects.all():
...   m.video.name = 'new_video_name.avi'
...   m.save()

What should I be doing here?

解决方案

There is no magic and easy way to do it. When you load your object from the database (the for loop), the name of the file referenced by the video attribute is loaded from the database too. You can't just change it and make the underlying file change it's name.

In order to accomplish a name change for both the physical file and the reference in the database you'll have to rename the physical file manually. Something like this should work:

import os

class SomeModel(models.Model):
    file_field = ... 

    def rename(self, new_name):
        old_path = self.file_field.path
        self.file_field.name = new_name

        os.rename(old_path, self.file_field.path)
        self.save()


for m in SomeModel.objects.all():
    new_name = compute_new_filename(m)
    m.rename(new_name)

Note that this is a simplified version and may want to do some handling for the possible IO errors when doing os.rename().

Also this code assumes that you are using the FileSystemStorage and not other custom storage class. In that case, the implementation will need to be adapted to that particular storage mechanism.

这篇关于上传文件后保存更新FileField文件(Django)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆