Can a wagtail ImageField be restricted to by image

2019-07-26 23:10发布

Can I set make a Wagtail ImageField only accept images of of certain dimensions ?

1条回答
不美不萌又怎样
2楼-- · 2019-07-26 23:53

Don't know if it's possible, but you can do something like this to restrict uploading images of large sizes:

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
查看更多
登录 后发表回答