Repository: unfoldadmin/django-unfold

Issue #355: actions_submit_line executed before the related models are saved
Created: 2024-04-15T18:05:54Z
I have a Django change form with a few inlines. I have created a actions_submit_line action that basically updates the status field in the model object and sends an email. The email is generated using the content from both parent and inline/child model fields. However, the email is missing the fields from the child model.
On further digging into the unfold repo, I noticed that the actions are called  from the save_model function and as per the django's core implementation save_related is called after the save_model which explains why the related model fields are not present/saved when the email is sent.
Currently I am overriding the save_model and save_related  in my modeladmin to workaround this issue.
    def save_model(self, request, obj, form, change) -> None:
        obj.save()
        
    def save_related(self, request, form, formsets, change):
        super().save_related(request, form, formsets, change)
        
        for action in self.get_actions_submit_line(request):
            if action.action_name not in request.POST:
                continue

            action.method(request, form.instance)

Should this be part of the unfold's core functionality, if not, what's the argument against it?

Duplicate issue #973: fix: submit line actions are executed before inline formsets are saved
Created: 2025-01-16T10:44:14Z
What version of Unfold are you using?
0.44.0
What version of Django are you using?
5.1
What browser are you using?
Not browser related
Did you checked changelog/commit history, if the bug is not already fixed?
Yes
Did you searched other issues, if the bug is not already fixed?
Yes
Did you checked documentation?
Yes
Are you able to replicate the bug in the demo site?
No, only usage of submit line actions I found was here https://demo.unfoldadmin.com/en/admin/formula/constructor/33/change/, but this admin page does not have any inlines.
Describe your issue
It seems that due to the nature that submit line actions are executed when model is saved, but before inline forms were saved, it's impossible to specify an action that for example will use both the main model, and it's children, as children data is not saved yet.
For example in my use case I had main model as read only page, and user needed to edit inline admin models in another tab, after editing I wanted to save & send API request to another server with new data entered in model inlines. However in my testing I found out that inline data was empty, at the time when I tried to initiate API request, but was saved after submit line action was executed. It happens only for the first time when data is empty, second time it will work, but I assume it would use old data, not the one that was submitted last.
I checked underlying django code for this, I assume the reason could be that submit line actions are happening in save_model method which saves only the main model, Django invokes another save_related method which saves additional formset data. It seems that in order for this to work properly, submit line actions should execute after both save_model and save_related methods have been executed.

Closing comment (2025-01-22T11:18:08Z): I believe this is a duplicate of #355. If you prepare a PR with a test. I will be happy to look at it.