Defer saving ClusterForm instances until formsets are saved#207
Draft
tomkins wants to merge 1 commit intowagtail:mainfrom
Draft
Defer saving ClusterForm instances until formsets are saved#207tomkins wants to merge 1 commit intowagtail:mainfrom
tomkins wants to merge 1 commit intowagtail:mainfrom
Conversation
Calling `super().save(commit=True)` triggers an `instance.save()` in Django's ModelForm code. This results in a cluster model that saves all child relations unexpectedly early, causing collisions or duplicates when the formset does get saved. By saving formsets first (without a commit), modelcluster is able to update the inlines before the instance gets fully saved.
Contributor
Author
|
Actually, hold off on the review a little bit. Need to ponder/tweak a bit for the "classic" M2M scenario. There's currently no cases covering it (only |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes wagtail/wagtail#13332
Fixes wagtail/wagtail#13701 (duplicate)
Description
Fixes IntegrityIssues or duplicate inlines when doing:
To test:
You'll see an IntegrityError 500 - which is wagtail/wagtail#13332.
You'll see: One, Dupe, Two - which is wagtail/wagtail#13701.
Install this branch:
Repeat the same instructions, and you'll no longer get an IntegrityError or duplicates.
This is due to wagtail/wagtail#10104. That pull request changed various forms to
.save(commit=True), which causes the following for the Revision model:Initial creation of a page - the primary key is stored for inlines:
Add an inline to the page:
The primary key for the new inline isn't saved, as the EditView saves the revision first, and then passes it to other code elsewhere to publish that (or, potentially publish later).
If you're editing that second version and just immediately publishing another one - it's all fine. However if you unpublish, Wagtail uses the stored Revision JSON when editing the page again, which has an empty PK.
Now, in theory this should all be handled by
django-modelcluster/modelcluster/forms.py
Lines 107 to 114 in 1b31175
django-modelcluster/modelcluster/forms.py
Lines 377 to 399 in 1b31175
Any call of
super().save(commit=True)results in:Because
super().save(commit=True)is effectivelyinstance.save(). The instance gets saved, all the inlines get saved, and then the inline formsets try to save.Anyway - I hope this is the right fix for the issue. Took me quite a lot of time to investigate this!
AI usage
Created the test myself (following other tests), then used Claude Opus 4.6 for the initial version of the fix (and changed/tweaked quite a lot before this PR).