django-bootstrap-modal-forms icon indicating copy to clipboard operation
django-bootstrap-modal-forms copied to clipboard

Delete view success message is shown although object not deleted

Open christianwgd opened this issue 2 years ago • 1 comments
trafficstars

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 avatar May 12 '23 07:05 christianwgd

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.

christianwgd avatar May 12 '23 10:05 christianwgd