views:

348

answers:

1

On Django's admin pages, I'd like to perform an action when the administrator clicks the Delete button for an object. In other words, I'd like to execute some code prior to arriving on the "Are you sure?" delete confirmation page.

I realize I could override the template page for this object, but I was hoping for something easier (i.e., override a method on the model or the form).

Any thoughts?

+2  A: 

You can override ModelAdmin.delete_view() method, like:

class MyModelAdmin(ModelAdmin):
    def delete_view(self, request, object_id, extra_context=None):
        # if request.POST is set, the user already confirmed deletion
        if not request.POST:
            perform_my_action()
        super(MyModelAdmin, self).delete_view(request, object_id, extra_context)
zgoda
you have to return super(MyModelAdmin, self....
panchicore