Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

actions_submit_line executed before the related models are saved #355

Open
afsangujarati93 opened this issue Apr 15, 2024 · 2 comments
Open

Comments

@afsangujarati93
Copy link

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
Copy link

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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants