更新ImageField时如何删除旧图像? [英] How to delete old image when update ImageField?

查看:643
本文介绍了更新ImageField时如何删除旧图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django创建一个股票照片网站,我的模型中有一个ImageField,问题是当用户更新图像字段时,原始图像文件不会从硬盘中删除。 p>

如何在更新后删除这些图像?



谢谢!

解决方案

您必须手动删除旧图像。



图像的绝对路径是存储在your_image_field.name中。所以你可以这样做:

  os.remove(your_image_field.name)

但是,为了方便起见,您可以使用相关的FieldFile对象,从而轻松访问底层文件,并提供一些方便的方法。请参阅 http://docs.djangoproject.com/en / dev / ref / models / fields /#filefield-and-fieldfile


I'm using Django to create a stock photo site, I have a ImageField in my model, the problem is that when the user update the image field, the original image file isn't deleted from the hard disk.

how can I make to delete those images after an update?

Thanks!

解决方案

You'll have to delete the old image manually.

The absolute path to the image is stored in your_image_field.name. So you'd do something like:

os.remove(your_image_field.name)

But, as a convenience, you can use the associated FieldFile object, which gives easy access to the underlying file, as well as providing a few convenience methods. See http://docs.djangoproject.com/en/dev/ref/models/fields/#filefield-and-fieldfile

这篇关于更新ImageField时如何删除旧图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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