Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

django

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?

like image 280
inlanger Avatar asked Feb 06 '15 11:02

inlanger


2 Answers

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

class SomeAdmin(admin.ModelAdmin):
    actions = (delete_selected,)
like image 107
inlanger Avatar answered Sep 28 '22 17:09

inlanger


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.

like image 24
Michel Sabchuk Avatar answered Sep 28 '22 19:09

Michel Sabchuk