6

I have a custom clean method below:

def clean_image(self):
    image = self.cleaned_data['image']
    if image:
        from django.core.files.images import get_image_dimensions
        w, h = get_image_dimensions(image)
        if not image.content_type in settings.VALID_IMAGE_FORMATS:
            raise forms.ValidationError(u'Only *.gif, *.jpg and *.png images are allowed.')
        if w > settings.VALID_IMAGE_WIDTH or h > settings.VALID_IMAGE_HEIGHT:
            raise forms.ValidationError(u'That image is too big. The image needs to be ' + str(settings.VALID_IMAGE_WIDTH) + 'px * ' + str(settings.VALID_IMAGE_HEIGHT) + 'px (or less).')
        return image

The problem scenario is this:

An image has been uploaded. I now want to clear it using the checkbox that appears using the ImageField widget. When submitting the form to make this clear take place the clear does not.

If I remove my custom clean method the clear does work. Therefore I guess my method is doing something wrong.

Triad sou.
  • 2,969
  • 3
  • 23
  • 27
user973347
  • 49
  • 1
  • 7
  • 3
    try un-indent the return so that it's inline with the if statement, i.e. it is always returning the data. – Timmy O'Mahony Sep 30 '11 at 18:16
  • How about [django-vimage](https://github.com/manikos/django-vimage) 3rd-party package? I have just created it and tries to solve such scenarios. Greetings :) – nik_m Apr 27 '18 at 17:16

2 Answers2

9

There are 3 problems when django realize this validations:

  1. Django need obtain the value of these field, always need return a value
  2. Need put class Meta with the name of the model you used.
  3. In this sentences need put .get this way

    self.cleaned_data.get['image']
    
  4. The code looks like this:

    class Meta: 
        model = NameModel    
    
    def clean_image(self):
        image = self.cleaned_data.get['image']
        if image:
            from django.core.files.images import get_image_dimensions
            w, h = get_image_dimensions(image)
            if not image.content_type in settings.VALID_IMAGE_FORMATS:
                raise forms.ValidationError(u'Only *.gif, *.jpg and *.png images are allowed.')
            if w > settings.VALID_IMAGE_WIDTH or h > settings.VALID_IMAGE_HEIGHT:
                raise forms.ValidationError(u'That image is too big. The image needs to be ' +     str(settings.VALID_IMAGE_WIDTH) + 'px * ' + str(settings.VALID_IMAGE_HEIGHT) + 'px (or less).')
        return image
    
Gianfranco Lemmo
  • 451
  • 5
  • 16
  • Point 1 is the answer here - you must ensure that `clean_image` returns a value, this allows for the Django admin clear function to occur. – jamesc Mar 01 '14 at 17:48
  • 2
    I was getting "Not subscriptable" errors until I changed the above to `image = self.cleaned_data['image']` – shacker Apr 20 '16 at 19:16
0

I think it isn't returning image from cleaned_data. You shoudl check that, because your coding looks fine.

Guilherme David da Costa
  • 2,318
  • 4
  • 32
  • 46