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