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

Delete view success message is shown although object not deleted #223

Open
christianwgd opened this issue May 12, 2023 · 1 comment
Open
Assignees
Labels
bug Something isn't working

Comments

@christianwgd
Copy link
Contributor

If I override the post method of the BSDeleteModal class to handle a ProtectedError (because a foreign key is still pointing to the object that should be deleted), the success message is shown although there's a redirect to another view.

I've made some extensions to the sample app to demonstrate the problem: https://github.com/christianwgd/django-bootstrap-modal-forms/tree/message_error
I've added a new object to the model that has a foreign key to a book. To avoid changes to the ui I added an admin for both models.

The only workaround for the problem I found by now was overriding the complete post method, setting the success message from there and set the class property success_message to None.

@christianwgd
Copy link
Contributor Author

After taking a closer look to the mixins, it seems like the only way to go is this one:

def post(self, request, *args, **kwargs):
    try:
        self.get_object().delete()
        messages.success(self.request, self.success_message)
    except models.ProtectedError:
        messages.error(
            self.request,
            'Cannot delete book due to existing reference'
        )
    return redirect(self.success_url)

If so and there's no better idea it would be fine to have some words on this in the docs and this issue could be closed. Let me know if I shall provide that as a PR.

@trco trco self-assigned this Jun 4, 2023
@trco trco added the bug Something isn't working label Jun 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants