替换 Django 图像不会删除原始图像
在 Django 中,如果模型中有 ImageFile,删除将从磁盘中删除关联的文件,并从数据库中删除记录。
替换图像不应该同时从磁盘中删除不需要的文件吗?相反,我看到它保留了原始内容并添加了替换内容。
现在删除对象不会删除原始文件,只会删除替换文件。
有什么好的策略可以做到这一点吗?如果我的用户经常替换他们的图像,我不想有一堆孤立文件。
In Django, if you have a ImageFile in a model, deleting will remove the associated file from disk as well as removing the record from the database.
Shouldn't replacing an image also remove the unneeded file from disk? Instead, I see that it keeps the original and adds the replacement.
Now deleting the object won't delete the original file only the replacement.
Are there any good strategies to doing this? I don't want to have a bunch of orphan files if my users replace their images frequently.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我发现的最佳策略是在模型中创建自定义保存方法:
并且要注意,与不删除后端文件的更新一样,删除实例模型(此处为照片)将不会
最后使用外键、多对多和其他关系测试所有更新/删除案例,以检查后端文件是否被正确删除。 只相信你所测试的。
The best strategy I've found is to make a custom save method in the model:
And beware, as with the updating which doesn't delete the back end file, deleting an instance model (here Photo) will not delete the back-end file, not in Django 1.3 anyway, you'll have to add more custom code to do that (or regularly do some dirty cron job).
Finally test all your update/delete cases with your ForeignKey, ManytoMany and others relations to check if the back-end files are correctly deleted. Believe only what you test.
在过去,
FileField
非常渴望清理孤立的文件。但这种情况在 Django 1.2 中发生了变化:In the olden days,
FileField
was eager to clean up orphaned files. But that changed in Django 1.2:以下工作示例中的代码将在 ImageField 中上传图像时检测是否存在同名文件,在这种情况下,在存储新文件之前删除该文件。
它可以很容易地修改,以便删除旧文件,无论文件名如何。但这不是我在项目中想要的。
添加以下类:
并将其与 ImageField 一起使用,如下所示:
The code in the following working example will, upon uploading an image in an ImageField, detect if a file with the same name exists, and in that case, delete that file before storing the new one.
It could easily be modified so that it deletes the old file regardless of the filename. But that's not what I wanted in my project.
Add the following class:
And use it with ImageField like so:
如果你不使用事务或者你不怕事务回滚时丢失文件,你可以使用 django-清理
If you don't use transactions or you don't afraid of loosing files on transaction rollback, you can use django-cleanup
关于这个问题已经有很多票了,尽管这很可能不会成为核心问题。最全面的是 http://code.djangoproject.com/ticket/11663。如果您正在寻找解决方案,补丁和票证评论可以为您提供一些指导。
您还可以考虑使用不同的 StorageBackend,例如 Django 片段 976 给出的覆盖文件存储系统。 http://djangosnippets .org/snippets/976/。您可以将默认存储更改为此后端,也可以在每个 FileField/ImageField 声明上覆盖它。
There have been a number of tickets regarding this issue though it is likely this will not make it into the core. The most comprehensive is http://code.djangoproject.com/ticket/11663. The patches and ticket comments can give you some direction if you are looking for a solution.
You can also consider using a different StorageBackend such as the Overwrite File Storage System given by Django snippet 976. http://djangosnippets.org/snippets/976/. You can change your default storage to this backend or you can override it on each FileField/ImageField declaration.
以下代码可以在有或没有
upload_to=...
或blank=True
的情况下工作,并且提交的文件与旧文件具有相同的名称。(py3 语法,在 Django 1.7 上测试)
请记住,这种解决方案仅适用于非事务上下文(无回滚,因为文件肯定会丢失)
Here is a code that can work with or without
upload_to=...
orblank=True
, and when the submitted file has the same name as the old one.(py3 syntax, tested on Django 1.7)
Remember that this kind of solution is only applicable if you are in a non transactional context (no rollback, because the file is definitively lost)
我使用了
popen
的简单方法,因此当我保存Info
模型时,我会在链接到新文件之前删除以前的文件:I used a simple method with
popen
, so when i save myInfo
model i delete the former file before linking to the new:我保存原始文件,如果它已更改 - 删除它。
I save the original file and if it has changed - delete it.