开发者

django - update images with ImageKit

On my project I'm using the ImageKit example provided at github: click here

I would like to be able to update the picture files if a picture for a specific user already exists (like the avatar) a开发者_如何学JAVAnd I'm not sure how exactly to go about that.I need to know how to delete pictures, save new ones and update the database accordingly. Any examples would be truly appreciated.


To create an image, I use save_image method. I don't know if it's different from the save method. But as far as I'm concerned, save_image method works fine for me.

To delete an image, just get the instance of that model and call delete method. It will delete both the database record and the images (including thumbnails). I need to mention that delete method on ImageModel works differently from ones on ordinary models. The difference I notice is that when you try to bulk delete images like Photo.objects.filter(owner = owner_id).delete(), the result will be that only database records are deleted, while files are retained. To completely delete an ImageModel, you need to get the exact instance and call delete method. In my example, you need to iterate through all the Photos and delete them one by one.

I think ImageModel is designed in a way that update is not encouraged. So in your case, if you want to replace an old avatar, the best way is create a new ImageModel instance, create a relationship with the user, save it and delete the original avatar.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜