当我从数据库/模型中删除对象时,如何让 Django 管理员删除文件?
我使用 1.2.5 和标准 ImageField 并使用内置存储后端。文件上传正常,但是当我从管理中删除条目时,服务器上的实际文件不会删除。
I am using 1.2.5 with a standard ImageField and using the built-in storage backend. Files upload fine but when I remove an entry from admin the actual file on the server does not delete.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
您可以接收
pre_delete
或post_delete
信号(请参阅下面 @toto_tico 的评论)并调用 delete() FileField 对象上的方法,因此(在 models.py 中):You can receive the
pre_delete
orpost_delete
signal (see @toto_tico's comment below) and call the delete() method on the FileField object, thus (in models.py):尝试 django-cleanup
settings.py
Try django-cleanup
settings.py
Django 1.5 解决方案:出于应用程序内部的各种原因,我使用 post_delete。
我把它放在 models.py 文件的底部。
original_image
字段是我的Photo
模型中的ImageField
。Django 1.5 solution: I use post_delete for various reasons that are internal to my app.
I stuck this at the bottom of the models.py file.
the
original_image
field is theImageField
in myPhoto
model.此代码在 Django 1.4 上也可以通过管理面板运行良好。
在删除模型之前获取存储和路径非常重要,否则如果删除模型,后者也将持续无效。
This code runs well on Django 1.4 also with the Admin panel.
It's important to get the storage and the path before delete the model or the latter will persist void also if deleted.
您需要在
删除
和更新
上删除实际文件。You need to remove the actual file on both
delete
andupdate
.您可以考虑使用 pre_delete 或 post_delete 信号:
https://docs.djangoproject.com/en/dev/topics /signals/
当然,取消 FileField 自动删除的原因同样适用于此。如果删除在其他地方引用的文件,则会遇到问题。
就我而言,这似乎很合适,因为我有一个专用的文件模型来管理我的所有文件。
注意:由于某种原因 post_delete 似乎无法正常工作。文件被删除,但数据库记录保留下来,这与我的预期完全相反,即使在错误情况下也是如此。不过 pre_delete 工作正常。
You may consider using a pre_delete or post_delete signal:
https://docs.djangoproject.com/en/dev/topics/signals/
Of course, the same reasons that FileField automatic deletion was removed also apply here. If you delete a file that is referenced somewhere else you will have problems.
In my case this seemed appropriate because I had a dedicated File model to manage all of my files.
Note: For some reason post_delete doesn't seem to work right. The file got deleted, but the database record stayed, which is completely the opposite of what I would expect, even under error conditions. pre_delete works fine though.
也许有点晚了。但对我来说最简单的方法是使用 post_save 信号。请记住,即使在 QuerySet 删除过程中也会执行信号,但在 QuerySet 删除过程中不会执行 [model].delete() 方法,因此它不是覆盖它的最佳选择。
核心/模型.py:
核心/信号.py
Maybe it's a little late. But the easiest way for me is to use a post_save signal. Just to remember that signals are excecuted even during a QuerySet delete process, but the [model].delete() method is not excecuted during the QuerySet delete process, so it's not the best option to override it.
core/models.py:
core/signals.py
此功能将在 Django 1.3 中删除,因此我不会依赖它。
您可以重写相关模型的
delete
方法,以在从数据库中完全删除条目之前删除该文件。编辑:
这是一个简单的例子。
This functionality will be removed in Django 1.3 so I wouldn't rely on it.
You could override the
delete
method of the model in question to delete the file before removing the entry from the database completely.Edit:
Here is a quick example.
使用 post_delete 肯定是正确的方法。有时虽然事情可能会出错,但文件不会被删除。当然,在这种情况下,您有一堆旧文件在使用 post_delete 之前没有被删除。我创建了一个函数,根据对象引用的文件是否不存在来删除对象的文件,然后删除对象,如果文件没有对象,则也删除,也可以根据对象的“活动”标志来删除对象..我添加到我的大多数模型中的东西。您必须向其传递要检查的对象、对象文件的路径、文件字段和删除不活动对象的标志:
这是您可以使用的方法:
Using the post_delete is for sure the right way to go. Sometimes though things can go wrong, and files don't get deleted. There is of course the case that you have a bunch of old files that weren't deleted before post_delete was used. I created a function that deletes files for objects based on if the file the object references does not exist then delete object, if the file does not have an object, then also delete, also it can delete based on an "active" flag for an object.. Something I added to most of my models. You have to pass it the objects you want to check, the path to the objects files, the file field and a flag to delete inactive objects:
This is how you can use this:
确保在文件前写入“self”。所以上面的例子应该是
我忘记了文件前面的“self”,并且它在全局命名空间中查找时不起作用。
make sure you write "self" before the file. so example above should be
I've forgotten the "self" before my file and that didn't work as it was looking in the global namespace.
如果您的项目中已有大量未使用的文件并想要删除它们,您可以使用 django 实用程序 django-未使用的媒体
If you already have number of unused files in your project and want to delete them, you can use django utility django-unused-media
Django 2.x 解决方案:
无需安装任何包!在 Django 2 中处理起来非常容易。我尝试过使用 Django 2 和 SFTP 存储的以下解决方案(但是我认为它适用于任何存储)
首先编写 自定义管理器。因此,如果您希望能够使用
objects
方法删除模型的文件,则必须编写并使用[自定义管理器][3](用于覆盖delete()
objects
的方法):现在,您必须先删除
image
,然后再删除模型本身,并且要将CustomManager
分配给模型,您必须初始化模型中的对象
:Django 2.x Solution:
There's no need to install any packages! It's very easy to handle in Django 2. I've tried following solution using Django 2 and SFTP Storage (however I think it would work with any storages)
First write a Custom Manager. So if you want to be able to delete files of a model by using
objects
methods, you must write and use a [Custom Manager][3] (for overridingdelete()
method ofobjects
):Now you must delete
image
before deleting deleting the model itself and for assigning theCustomManager
to the model, you must initialobjects
inside your model:我可能有一个特殊情况,因为我在带有动态目录名称的文件字段上使用 upload_to 选项,但我找到的解决方案是使用 os.rmdir。
在模型中:
...
I may have a special case since I am using the upload_to option on my file field with dynamic directory names but the solution I found was to use os.rmdir.
In models:
...