How to delete old image when update ImageField?

前端 未结 8 1573
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 09:20

I\'m using Django to create a stock photo site, I have an ImageField in my model, the problem is that when the user updates the image field, the original image file isn\'t d

8条回答
  •  一生所求
    2020-12-13 09:48

    You'll have to delete the old image manually.

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

    os.remove(your_image_field.path)
    

    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

提交回复
热议问题