1

I have the following model:

class PropertyPhoto(models.Model):
    property = models.ForeignKey('Property')
    photo_show = ProcessedImageField(upload_to=get_web_watermark,
                                  processors=[Watermark()],                                     
                                  options={'quality': 95})

and custom Watermark class:

class Watermark(object):    

    def process(self, image):
        try:
            this_property = Property.objects.get(pk=*self.property.id*)
        except Property.DoesNotExist:
            print "error"

How do I access my model attribute "property" from inside Watermark class?

Thank you

Bih Cheng
  • 655
  • 1
  • 13
  • 28

1 Answers1

2

You'll need to create a custom spec that gets the information you need from the model and passes it to your processor. The docs show an example:

from django.db import models
from imagekit import ImageSpec, register
from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFill
from imagekit.utils import get_field_info

class AvatarThumbnail(ImageSpec):
    format = 'JPEG'
    options = {'quality': 60}

    @property
    def processors(self):
        model, field_name = get_field_info(self.source)
        return [ResizeToFill(model.thumbnail_width, thumbnail.avatar_height)]

register.generator('myapp:profile:avatar_thumbnail', AvatarThumbnail)

class Profile(models.Model):
    avatar = models.ImageField(upload_to='avatars')
    avatar_thumbnail = ImageSpecField(source='avatar',
                                      id='myapp:profile:avatar_thumbnail')
    thumbnail_width = models.PositiveIntegerField()
    thumbnail_height = models.PositiveIntegerField()

Note that you can pass the spec directly to the ImageSpecField constructor (using the spec kwarg) instead of registering it with an id (and using the id kwarg) as shown above.

matthewwithanm
  • 3,733
  • 2
  • 21
  • 27