Django storring file. Detect new file
I'm trying to subclass ImageField
.
Basically my MyImageField
have to save two pictures in storage when uploading one picture. First it's original picture with name blablabla.jpg
and second is the actually thumbnail whith name blablabla_thumb.jpg
. but in databse i'll have only one field with path to oroginal picture. To get picture thumbnale i'll create method of MyImageField which call tumb when i do model.MyImageFiled.thumb it returns me path to file with prefix thumb
.
Question is how to detect if FileField
is new file or just model is saving without changing file. I need it to decide if i need to make new thumbnail (cropping,converting and stuff) or not. So if user just put MyFileField
in his model and than he makes change to name o开发者_StackOverflowr description in model it not make file thumb again.
Only way I see here is to save date created and compare. If different make thumb, otherwise don't.
That djangothumbnails.com never was anything good and now it's not only that but also unmaintained, better use sorl-thumbnail or easy-thumbnails.
this maybe work for you Django Thumbnails
It's a general problem I think -- how to detect in the model save() method whether a file has just been uploaded. Typically this would be so that you can do some once-only processing (e.g. producing a thumbnail) the first time you see the file.
The solution I use, which I do not like, is to do the processing and then prefix the uploaded file name with a known string ("p$") before calling super(MyModel, self).save() to store it in the DB.
Whenever I get a file upload, I look to see if it starts with "p$", indicating that no processing needs to be done.
I think Django should provide its own flag someplace to indicate that an upload has just occurred.
精彩评论