How to delete old image when update ImageField?

前端 未结 8 1598
被撕碎了的回忆
被撕碎了的回忆 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:45

    Before updating the model instance, you can use the delete method of FileField object. For example, if the FileField or ImageField is named as photo and your model instance is profile, then the following will remove the file from disk

    profile.photo.delete(False)
    

    For more clarification, here is the django doc

    https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.fields.files.FieldFile.delete

提交回复
热议问题