unfoldadmin / django-unfold

Modern Django admin theme for seamless interface development
https://unfoldadmin.com
MIT License
1.04k stars 96 forks source link

actions_submit_line executed before the related models are saved #355

Open afsangujarati93 opened 4 weeks ago

afsangujarati93 commented 4 weeks ago

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?

justinqian42 commented 3 weeks ago

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.

lukasvinclav commented 1 week ago

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