当删除具有ImageField字段的对象时,图像文件未被删除

问题描述:

我有一个模型图片 ImageField 。删除 Picture 的实例时, ImageField 中设置的文件不会被删除。

I've got a model Picture with an ImageField. When deleting an instance of Picture, the file set in the ImageField is not deleted.

这是一个bug吗?如何执行?

Is it a bug? How can I do it?

删除与 FileField s和 ImageField 在Django 1.3中有意删除。请参阅门票#6456 。 Django广泛使用事务来防止请求发生错误的数据损坏。如果删除事务被回滚,则在文件从文件系统中删除后,您现在有一个记录指向不存在的文件。所有要求自动删除的票据已经被简要标记为不会修复,所以这不会改变。

Deletion of files associated with FileFields and ImageFields was intentionally removed in Django 1.3. See ticket #6456. Django uses transactions extensively to prevent data corruption if something goes wrong with the request. If a deletion transaction is rolled-backed, after the file has been deleted from the filesystem, then you now have a record pointing to a non-existent file. All tickets asking for automatic deletion to return have been summarily marked "Won't Fix", so this is not going to change.

有关变通方法,请参阅此前StackOverflow问题

For workarounds, see this previous StackOverflow question.