Reputation:
I'm using ImageWithThumbnailsField from sorl to handle images on my project, without any problems. Now I'd like to use PIL to generate images out of my text, in some cases that the user doesn't upload photos. I'm having a bad time trying to figure what is the code, so any help would be appreciated!
step 1. when user doesn't upload photo, create one with PIL (this is done)
step 2. assign the created photo as the ImageWithThumbnailsField (your help goes here)
Thanks!
Upvotes: 2
Views: 873
Reputation: 365
It's hard to say without an example of what you are currently doing but here is what I think should work.
# retrieve the Model instance however your app requires
m = YourModel.objects.get(pk=1)
# retrieve the upload_to path from the Field definition
upload_to = m.img_with_thumb.field.upload_to
# update field with location of the new image
# img_filename should be whatever PIL created in step 1
m.img_with_thumb = os.path.join(upload_to, img_filename)
# save the model
m.save()
I have never tried this with sorl, but it works with a regular Django ImageField.
Upvotes: 2