question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InlinePanel Image field data is lost on subsequent saves

See original GitHub issue

Issue Summary

InlinePanel ImageField (and possibly FileField) data is lost on saves if the previous revision had not been published

Steps to Reproduce

  1. 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'),
    ]
  1. Create a new ProjectPage and add a single ProjectPageImage.
  2. Save draft
  3. Add another ProjectPageImage, and click Save Draft
  4. 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:open
  • Created 7 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
liuqcommented, Jun 21, 2019

I also ask whether there are news about this issue.

1reaction
lelitcommented, Feb 1, 2019

I hit the same problem, trying to implement a per-page image gallery. Any news on the matter?

Read more comments on GitHub >

github_iconTop Results From Across the Web

file field doesn't gets saved with wagtail inline field
I'm trying to user inline panels but for plain Django model in wagtail like it's described here. I got it working with a...
Read more >
How To Not Put Data In Imagefield In Django? - ADocLib
Issue Summary InlinePanel ImageField and possibly FileField data is lost on saves if the previous revision had not been published Steps to.
Read more >
Creating an interactive event budgeting tool within Wagtail
Wagtail provides an InlinePanel solution that allows nested inline data relations to be edited easily. We will have two core models, the ...
Read more >
Wagtail 1.7 release notes
Custom image models require a data migration for the new filter_spec field¶ · Run manage.py makemigrations to create the schema migration · Run...
Read more >
Using images in templates - Wagtail Documentation
Wagtail can assign the image data to another variable using ... use multiple tabs of fields, nor do they provide the “save as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found