django-unfold icon indicating copy to clipboard operation
django-unfold copied to clipboard

actions_submit_line executed before the related models are saved

Open afsangujarati93 opened this issue 10 months ago • 2 comments

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?

afsangujarati93 avatar Apr 15 '24 18:04 afsangujarati93

I agree with you. The logic behind the design is to execute the submit-line actions after the object is saved, along with which one implication is that all the relations should also be saved as well.

justinqian42 avatar Apr 17 '24 00:04 justinqian42

@afsangujarati93 would you mind to prepare a PR with an appropriate test? Definitely this is something what can be merged.

lukasvinclav avatar May 03 '24 17:05 lukasvinclav

@justinqian42 @afsangujarati93 would you mind to prepare a PR? Because I'm not 100% sure what is wrong and I need a help to understand the problem more.

lukasvinclav avatar Jul 05 '24 14:07 lukasvinclav