views:

377

answers:

1

Hi there,

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!

+1  A: 

It's hard to say without an example of what you are currently doing but here is what I think should work.

  1. Make sure the PIL created photo is saved to the location specified in the 'upload_to' parameter of the ImageWithThumbnailsField on your model.
  2. Update the related model instance using something like the following:
    # 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.

moberley