可以通过图像尺寸限制wagtail ImageField

时间:2018-03-19 15:30:21

标签: python django wagtail imagefield

我可以设置Wagtail ImageField仅接受某些尺寸的图像吗?

1 个答案:

答案 0 :(得分:0)

不知道是否可行,但你可以做这样的事情来限制上传大尺寸的图片:

from wagtail.wagtailadmin.forms import WagtailAdminPageForm


class BlogPageForm(WagtailAdminPageForm):

    def clean(self):
        cleaned_data = super().clean()

        if (cleaned_data.get('image') and somehow_check_if_img_to_large():
            self.add_error('image', 'Error! image is to large!')

        return cleaned_data


class BlogPage(Page):
    image = models.ForeignKey(
        'wagtailimages.Image',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+'
    )
    description = RichTextField(blank=True)

    content_panels = Page.content_panels + [
        FieldPanel('photos'),
        FieldPanel('description')
    ]
    base_form_class = BlogPageForm