Django: how to install the prohibition on removal of an object through the admin panel?

Good afternoon.

Want after clicking on the delete button in the admin area, to do some "testing" if the test is true then the entry is removed if not then return to the same page with the error (in the likeness of ValidationError), where a message Ala "the record cannot be deleted because ...".
In admin.ModelAdmin has a method has_delete_permission while using it, it is responsible for displaying the "delete"button:
def has_delete_permission(self, request, obj=None):
 if obj:
 a = 1 
 if a:
 return True
 return False

But it's not quite what I need, I want the button to be always, but permission to remove it or not after pressing it. Form validation works on this principle.

You could do:
def delete_model(self, request, obj):
 if True:
 raise Exception

But, so from just throws the error page, not the post editing page with the error, besides delete_model as save cannot be used to block removal of the object, only to add actions before and after removal.

Thank you for participating!
June 8th 19 at 17:29
1 answer
June 8th 19 at 17:31
besides delete_model as save cannot be used to block removal of the object, only to add actions before and after removal.

This is absolutely not true. The model will be deleted if this method is overridden to explicitly call super().delete_model(request, obj), otherwise - no.
You can make so that messages framework has not captured the standard message about the successful removal that is sent to the admin with the intermediate page and add your:

from django.contrib import messages

def delete_model(self, request, obj):
 """If some validation is successful - to remove the object, if not, show an error message."""
 if check_permission(obj):
 return super().delete_model(request, obj)
 # In the following query captures only errors.
 # So after clicking the delete button will not be captured about the message
 # about the successful deletion.
 messages.set_level(request, messages.ERROR)
 message = "You cant delete this!"
 # Send a message about the error.
 self.message_user(request, message, level=messages.ERROR)

Well, more to do something with the logging, because "delete" will still fall into the "recent activity" on the main admin page, though the object was not deleted.
This is absolutely not true.

That's where I read:

Yes, if you do not call super, he did not remove, but apparently gangsta specifically warn that would not do probably due to the fact that the meaning of these methods is laid down in what to remove/keep with intermediate action, like this concept. But, Yes you can use them for other purposes, but this is not encouraged. - Alyson_Greenho commented on June 8th 19 at 17:34
on message_user I also have noticed - Alyson_Greenho commented on June 8th 19 at 17:37
Not that is not welcome, in of the dock, it explicitly says that this method must delete the object and is not intended to some sort of Talbakov if something is wrong. But if you need to do is che really there, we hounokidaira)
When overriding ModelAdmin.save_model() and ModelAdmin.delete_model(), your code must save/delete the object. They aren't meant for veto purposes, rather they allow you to perform extra operations.
- alexandro_Auer commented on June 8th 19 at 17:40
I want that would look nice initially) - Alyson_Greenho commented on June 8th 19 at 17:43

Find more questions by tags PythonDjango