jonasundderwolf / django-image-cropping

Django helper application to easily and non-destructively crop arbitrarily large images in admin and frontend.
Other
553 stars 131 forks source link

Empty ForeignKey image field causes AttributeError #59

Closed huogerac closed 9 years ago

huogerac commented 9 years ago

Considering the following models:

class GalleryItem(models.Model):
    name = models.CharField(max_length=255, null=True, blank=True)
    picture = models.ImageField(upload_to='pictures/%Y/%m/%d', blank=True)

class NewsItem(models.Model):
    title = models.CharField(max_length=255)
    picture = models.ForeignKey(GalleryItem, null=True, blank=True)
    cropping = ImageRatioField('picture__picture', '120x100')

When we try to add a new register with an empty picture (because it's not required) we get the following error:

...image_cropping/fields.pyc in initial_cropping(self, sender, instance, *args, **kwargs)
    101             if ratiofield.image_fk_field :  # image is ForeignKey
    102                 # get the imagefield
--> 103                 image = getattr(image, ratiofield.image_fk_field)
    104             if not image:
    105                 continue

AttributeError: 'NoneType' object has no attribute 'picture'

I'm using: Django==1.6.6 django-image-cropping==0.9