InlinePanel Image field data is lost on subsequent saves
See original GitHub issueIssue Summary
InlinePanel ImageField (and possibly FileField) data is lost on saves if the previous revision had not been published
Steps to Reproduce
- Create a Page model and a related model, e.g.:
class ProjectPage(Page):
content_panels = Page.content_panels + [
InlinePanel('images')
]
class ProjectPageImage(Orderable):
page = ParentalKey(ProjectPage, related_name="images")
image = ImageField()
caption = models.CharField(max_length=1024, blank=True)
panels = [
FieldPanel('image'),
FieldPanel('caption'),
]
- Create a new ProjectPage and add a single ProjectPageImage.
- Save draft
- Add another ProjectPageImage, and click Save Draft
- Edit one of the captions on the 2nd image (or any other field on the ProjectPage that is not the image field) and Save Draft.
The Wagtail admin will throw up an error saying that the image is required on the 2nd image, instead of allowing the save to go through instead.
I’ve had a poke around, and it looks like a weird interaction between Wagtail, page revisions and django-modelcluster.
Basically, when editing a page, the ‘edit’ view (in wagtail/wagtailadmin/views/pages.py) gets the latest revision as the ‘instance’.
The first time the ProjectPage is saved, it creates and saves the ‘ProjectPageImage’ model to the database, so that it has a primary key.
If the ProjectPage is not published on subsequent saves, any ProjectPageImage that is added will be stored in the PageRevision as having “None” for the ProjectPageImage pk.
On the next save, if something is updated on the form, the modelcluster.forms.BaseTransientModelFormSet will have is_bound = True, as there have been data changes made.
When _construct_form() is called on the BaseTransientModelFormSet, as is_bound = True, and the pk value of the ProjectPageImage is None (as it’s on a revision that has not yet been published), it will just self.model() as the instance, instead of the actual model.
Subsequently, ImageField is not given the existing file as “initial” data, and will think that the field is empty.
Technical details
- Python version: 2.7.12
- Django version: 1.10.3
- Wagtail version: 1.7
- Browser version: Chrome 54
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (2 by maintainers)
Top GitHub Comments
I also ask whether there are news about this issue.
I hit the same problem, trying to implement a per-page image gallery. Any news on the matter?