inlanger
inlanger

Reputation: 2986

How to skip delete confirmation page in Django admin for specific model?

I have a model with a huge amount of data, and Django creates delete confirmation page a very long time. I have to skip this process and delete data without any confirmation. I have tried some solutions from the internet, but it doesn't work - I still see confirmation page.

Anyone know how to do that?

Upvotes: 7

Views: 3163

Answers (2)

Michel Sabchuk
Michel Sabchuk

Reputation: 840

Django 2.1 released the new ModelAdmin method get_deleted_objects that allows to specify parameters to the confirmation screen for both single and multiple delete (e.g. using the delete action).

In my case, I wanted to delete a list of objects with several relationships, but set with cascade deletion. I ended up with something like this:


def get_deleted_objects(self, objs, request):                                                                                                                                
      deleted_objects = [str(obj) for obj in objs]
      model_count = {MyModel._meta.verbose_name_plural: len(deleted_objects)}
      perms_needed = []
      protected = []
      return (deleted_objects, model_count, perms_needed, protected)

I could include other models in the model_count dict, getting only the count, for example, to still avoid list thousands of minor instances that I don't need to see individually.

Upvotes: 5

inlanger
inlanger

Reputation: 2986

def delete_selected(modeladmin, request, queryset):
    queryset.delete()

class SomeAdmin(admin.ModelAdmin):
    actions = (delete_selected,)

Upvotes: 3

Related Questions