Delete confirmation not appending.

Delete confirmation screen is not appending while doing delete action using custom delete action for inactive records from listing screen.

Is this a bug?

Clarify your templates, what are you trying to do for which page there isn’t much you have provided like

What is this? like a page? or a Modal pop up?. Share your particular template where you are implementing this.

That confirmation screen isn’t something “automatic”. The Django admin delete action sends the confirmation page as the response to that action. Take a look at how that works in django.contrib.admin.actions.

If you want the confirmation page to be used, you need to do something similar to what it’s doing.

Following are my requirements:

How do I override the admin model delete action with confirmation beforehand?

I am trying to modify the delete action that comes default with any registered model in the admin.

My goal is to override it and add in some extra functionality. This has actually been successful, but I lose the delete confirmation page beforehand. What happens instead is that the delete action fully executes and returns me back to the model’s list view immediately. I want to keep the confirmation page as an intermediary before my action goes ahead and performs its customized deletion behavior on my selected objects.

Unfortunately, I’ve gotten a bit stuck on how to do this. Does anyone know how it can be done?

So far, my code is as follows:

actions = [‘delete_queryset’]
def delete_queryset(self, request, queryset):
“”"
delete_queryset method is using for id selected inactive record is exists then delete it otherwise it’s not.
“”"
inactive_records = queryset.filter(status=‘I’)
count_inactive_records = len(inactive_records)
if inactive_records.exists():
inactive_records.delete()
if count_inactive_records == 1:
self.message_user(request, f"Successfully deleted Inactive capability.“, level=messages.SUCCESS)
else:
self.message_user(request, f"Successfully deleted {count_inactive_records} Inactive capabilities.”, level=messages.SUCCESS)
else:
self.message_user(request, ‘Active capability could not be deleted. Please select the inactive capability to perform delete operation.’, level=messages.WARNING)
delete_queryset.short_description = “Delete selected inactive capabilities”

I have implemented the custom delete action in listing screen to delete the inactive records.
Using overriding the delete_queryset method in model_admin for model.
But before deleting the records i want to show a delete confirmation page where user can confirm the deletion of the inactive record clicking on yes or no buttons. like django admin provides for its delete action.
I had already override the delete_confirmation.html and delete_selected_confirmation.html in my django application template directory.

You then need to add the logic into your code to use those templates.

Your custom action needs to do two different things depending upon whether the request is a GET or a POST.

If the request is a GET, display the confirmation page and not delete the rows. This confirmation page is a form, which will submit a POST request if the confirmation button is pressed.

If the request is a POST, then you delete the data.

Again, I suggest you look at the Django source code to see how the admin does this. It’s the delete_selected function in django.contrib.admin.actions.