Code used to override the delete_queryset in the modeladmin:
def get_actions(self, request): actions = super().get_actions(request) del actions['delete_selected'] return actions def really_delete_selected(self, request, queryset):''' # 1. Update the group-mailbox relation: `goto` values if mailbox(es) are deleted # 2. Sending a mail to the current user with CSV of mailboxes deleted # 3. Used domain space will reduce by the maxquota of mailbox''' response = None print('delete from mailbox queryset called') try: mbox_list = [] failed_deletions = [] print(queryset) for obj in queryset: mdomain = None # COMPLETE MAILBOX DELETION OF FOLDERS, DOVECOT ENTRY, ADMIN PANEL PERMANENTLY api_delete_status = call_purge_mailbox_api(request,obj) response = api_delete_status print('response---------',response, response['status_code']) if response['status_code'] == 200: # Set the quota value after deletion of mailbox(es) mdomain = Domain.objects.get(id=obj.domain.pk) mdomain.quota -= obj.maxquota mdomain.save() mbox_list.append(obj.email) # Remove the user from the group mailing lists # master_grp_list = GroupMailIds.objects.exclude(goto=None) removed_from_groups :bool = remove_from_group_lists(obj.email,mbox_list) print('mailbox deletion completed.....') # TODO: List for sending a mail to the currently logged in user with CSV of # mailboxes deleted else: print('Failed to delete mailbox:', obj.email) failed_deletions.append(obj.email) print(failed_deletions, len(failed_deletions)) if mbox_list: # Check if any mailboxes were successfully deleted self.message_user(request, f"Successfully deleted {len(mbox_list)} mailbox(es).",level='success') for email in failed_deletions: # Display error message for each failed deletion self.message_user(request, f"Failed to delete {email}. Mailbox absent. Contact helpdesk.",level='error') if not mbox_list and not failed_deletions: # Check if no deletions were attempted self.message_user(request, "No mailboxes were selected for deletion.",level='error') #return messages.error(request, "No mailboxes were selected for deletion.") except Exception as e: print(e) self.message_user(request, f"Failed to delete items: {str(e)}", level='error') really_delete_selected.short_description = "Delete selected entries"def delete_model(self, request, obj): try: if obj.id is None: # If the object's ID is None, it means the object doesn't exist self.message_user(request, "Failed to delete object. Object does not exist.", level='error') #return HttpResponseRedirect(request.path) # Perform the deletion api_delete_status = call_purge_mailbox_api(request, obj) if isinstance(api_delete_status, dict) and 'status_code' in api_delete_status: status_code = api_delete_status['status_code'] if status_code == 200: # If the deletion was successful, trigger response_delete return self.response_delete(request, str(obj), None) # If the deletion operation did not succeed, handle the failure self.message_user(request, f"Failed to delete mailbox {obj}. Contact Helpdesk", level='error') except Exception as e: # Handle deletion failure self.message_user(request, f"Failed to delete {obj}. Error: {str(e)}", level='error') # Redirect back to the change list page after deletion attempt return None #HttpResponseRedirect(request.path) def response_delete(self, request, obj_display, obj_id): # Override response_delete to handle response after deleting via action response = super().response_delete(request, obj_display, obj_id) print('response_deleteeeee', response, response.status_code) # Check if the deletion failed if response.status_code != 200: self.message_user(request, f"Failed to delete {obj_display}.", level='error') return responseThis code is displaying correctly for the delete_queryset on failure of deletion. But when we delete the mailbox object in the model admin page after confirm_delete(confirm_delete.html), the message displayed after returning to the listing page are:
Failed to delete mailbox yy@domain1.net. Contact Helpdesk
as well as
The mailbox “yy@domain1.net” was deleted successfully.
Failure is the expected result. But why does django also display successful deletion? How to hide this message in the modeladmin listing page for failed cases?
Deletion from queryset works as expected. Displays the failed message only