django-cmsdjango-filer

Django-CMS FilerImageField: Validator function


I'm trying to write a validator for a Django-CMS FilerImageField. The following validator function is used for a default ImageField. When I copy it to the new Model, it will crash with the message 'int' object has no attribute 'file'. Obviously a different type of value is being passed to the validator function. I can't seem to find information on what kind of data is being passed to the validator. How do I correctly reference the file so I can get_image_dimensions()?

def validate(fieldfile_obj):
    width, height = get_image_dimensions(fieldfile_obj.file) #crash
    if width > 1000:
        raise ValidationError("This is wrong!")

Solution

  • Ok I found it. fieldfile_obj in this case contains the primary key of a Image record. The solution was to get an instance of filer.models.Image and pass this instance's file property to the validator function:

    from filer.models import Image
    
    # ... code ...
    
    def validate(fieldfile_obj):
        image = Image.objects.get(pk=fieldfile_obj)
        width, height = get_image_dimensions(image.file)
        if width > 1000:
            raise ValidationError("This is wrong!")