Gua*_*ard 82 python django file
我在磁盘上有一个现有文件(例如/folder/file.txt)和Django中的FileField模型字段.
当我做
instance.field = File(file('/folder/file.txt'))
instance.save()
Run Code Online (Sandbox Code Playgroud)
它将文件重新保存为file_1.txt(下一次_2,等等).
我理解为什么,但我不想要这种行为 - 我知道我希望该字段与之关联的文件真的在那里等着我,我只想让Django指向它.
怎么样?
bar*_*ara 104
只需设置instance.field.name文件的路径即可
例如
class Document(models.Model):
file = FileField(upload_to=get_document_path)
description = CharField(max_length=100)
doc = Document()
doc.file.name = 'path/to/file' # must be relative to MEDIA_ROOT
doc.file
<FieldFile: path/to/file>
Run Code Online (Sandbox Code Playgroud)
Bur*_*lid 20
如果要永久执行此操作,则需要创建自己的FileStorage类
import os
from django.conf import settings
from django.core.files.storage import FileSystemStorage
class MyFileStorage(FileSystemStorage):
# This method is actually defined in Storage
def get_available_name(self, name):
if self.exists(name):
os.remove(os.path.join(settings.MEDIA_ROOT, name))
return name # simply returns the name passed
Run Code Online (Sandbox Code Playgroud)
现在在您的模型中,您使用修改后的MyFileStorage
from mystuff.customs import MyFileStorage
mfs = MyFileStorage()
class SomeModel(model.Model):
my_file = model.FileField(storage=mfs)
Run Code Online (Sandbox Code Playgroud)
小智 12
试试这个(doc):
instance.field.name = <PATH RELATIVE TO MEDIA_ROOT>
instance.save()
Run Code Online (Sandbox Code Playgroud)
编写自己的存储类是正确的。但是,get_available_name这不是重写的正确方法。
get_available_name当Django看到具有相同名称的文件并尝试获取新的可用文件名时,将调用。不是导致重命名的方法。造成的方法是_save。中的注释_save非常好,您可以轻松找到带有标志的打开待写入文件,os.O_EXCL如果已经存在相同的文件名,则会引发OSError。Django捕获此错误,然后调用get_available_name以获取新名称。
所以我认为正确的方法是_save不带flag 覆盖并调用os.open()os.O_EXCL。修改很简单,但是方法有点长,所以我不在这里粘贴。告诉我您是否需要更多帮助:)