diff --git a/wagtail/images/blocks.py b/wagtail/images/blocks.py index dc9378c677..b6fc277be2 100644 --- a/wagtail/images/blocks.py +++ b/wagtail/images/blocks.py @@ -138,7 +138,7 @@ class ImageBlock(StructBlock): def to_python(self, value): # For backward compatibility with ImageChooserBlock - if isinstance(value, int): + if value is None or isinstance(value, int): image = self.child_blocks["image"].to_python(value) struct_value = {"image": image, "decorative": False, "alt_text": None} else: diff --git a/wagtail/images/tests/test_blocks.py b/wagtail/images/tests/test_blocks.py index 6878678855..64633649ad 100644 --- a/wagtail/images/tests/test_blocks.py +++ b/wagtail/images/tests/test_blocks.py @@ -248,6 +248,13 @@ class TestImageBlock(TestImageChooserBlock): self.assertEqual(result.contextual_alt_text, "Sample text") self.assertFalse(result.decorative) + def test_to_python_with_none(self): + # Like the test_to_python_with_int case, this can occur when a non-required + # ImageChooserBlock has been changed to an ImageBlock + block = ImageBlock(required=False) + value = block.to_python(None) + self.assertIsNone(value) + def test_bulk_to_python_with_empty_list(self): block = ImageBlock(required=False) result = block.bulk_to_python([])