Django-CMS FilerImageField:验证器函数

Django-CMS FilerImageField: Validator function

我正在尝试为 Django-CMS 编写验证器 FilerImageField。以下验证器函数用于默认 ImageField。当我将它复制到新模型时,它会崩溃并显示消息 'int' object has no attribute 'file'。显然,一种不同类型的值被传递给验证器函数。我似乎无法找到有关传递给验证器的数据类型的信息。我该如何正确引用该文件才能 get_image_dimensions()?

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

好的,我找到了。 fieldfile_obj 在这种情况下包含图像记录的主键。解决方案是获取 filer.models.Image 的实例并将该实例的 file 属性 传递给验证器函数:

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!")