Search code examples
pythondjangowagtailimagefield

Can a wagtail ImageField be restricted to by image dimension


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


Solution

  • 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