Can a wagtail ImageField be restricted to by image dimension

此生再无相见时 提交于 2019-12-11 06:23:51

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/49366684/can-a-wagtail-imagefield-be-restricted-to-by-image-dimension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!