Opened 7 years ago

Last modified 6 weeks ago

#15522 new New feature

Allow customizing queryset deletion in the delete_selected admin action view

Reported by: Divad Owned by: nobody
Component: contrib.admin Version: master
Severity: Normal Keywords: delete_model
Cc: Sergiy Kuzmenko, omerd1, commonzenpython@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Based on [Ticket #11108]

The correction should also include this change in

<                 modeladmin.delete_model(request,obj)
>             queryset.delete()

To have the same result using delete_selected from actions.

Change History (17)

comment:1 Changed 7 years ago by Divad <david@…>

Component: Uncategorizeddjango.contrib.admin

comment:2 Changed 7 years ago by Julien Phalip

Resolution: invalid
Status: newclosed

I don't think this is relevant as the delete action performs a bulk delete on the queryset instead of calling delete() for each object.

If you think I'm mistaken, please reopen but also provide more detailed information (e.g. description of a use case or code samples) about what you're trying to achieve.

comment:3 Changed 7 years ago by Divad <david@…>

Resolution: invalid
Status: closedreopened

I have defined historic models with three states Current / Changed / Deleted, t\
agged with user and timestamp of operation.
so i have defined "delete_model" as:

    def delete_model(self, request, obj):
	obj.user = request.user
	super(HistoricModelAdmin,self).delete_model(request, obj)

this allows to track the user who deletes an object.

On the other end when bulk delete is triggered with delete action, I do not kno\
w the user who triggered the action, resulting in two different results.

I suppose there could be other users unaware of this two different results to t\
he same action.

But someone could argue that I should define my own delete action instead of us\
ing the default one.

comment:4 Changed 7 years ago by Julien Phalip

Triage Stage: UnreviewedAccepted

Thanks for the details. I can see the use of knowing which user has performed the bulk delete. However delete_model(self, request, obj) cannot be used since that method is for one specific object.

As you mentioned, an easy workaround would be to define your own delete action, which you could then wrap around the built-in one.

I agree that it would be nice to have an easier way of achieving that in core, so I'm marking as Accepted.

comment:5 Changed 7 years ago by Julien Phalip

Needs documentation: set
Needs tests: set
Patch needs improvement: set

comment:6 Changed 7 years ago by Divad <david@…>

The def delete_model(self, request, obj) is attached with a class inherited from admin.ModelAdmin.

So my original idea of changing bulk delete with queryset.delete() to a modeladmin.delete_model(request,obj) for each object.

I realize that this can be inefficient but is the only way to ensure consistent behavior for both ways of deleting in admin.

comment:7 Changed 7 years ago by Łukasz Rekucki

Severity: Normal
Type: New feature

comment:8 Changed 6 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:9 Changed 6 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:10 Changed 6 years ago by Sergiy Kuzmenko

Cc: Sergiy Kuzmenko added

comment:11 Changed 6 years ago by omerd1

Cc: omerd1 added

I've encountered this 'feature' while debugging my delete_model.

Another working workaround is to register for pre_delete signal

comment:12 Changed 5 years ago by Aymeric Augustin

Status: reopenednew

comment:13 Changed 16 months ago by Paulo

While I agree that we should keep consistency, in this case I think both actions should be treated separately.
One deletes a single object, the other deletes multiple objects.
Hooking a pre delete signal or calling delete_model have big impact on performance depending on how many objects
are being deleted.

I'd like to propose a change in the delete action that allows for hooking into it before the deletion takes place,
maybe a method in the ModelAdmin class?

comment:14 Changed 16 months ago by Paulo

Cc: commonzenpython@… added

comment:15 Changed 12 months ago by Tim Graham

Summary: delete_model on admin delete actionAllow customizing queryset deletion in the delete_selected admin action view

#27326 is a duplicate with a patch (but without tests and docs).

comment:16 Changed 7 weeks ago by vangelou

Needs documentation: unset
Needs tests: unset

The following pull request is based on ticket #27326. ModelAdmin exports a delete_queryset(self, request, queryset) method which can be overriden allowing extra actions during the queryset deletion process.

comment:17 Changed 6 weeks ago by vangelou

Patch needs improvement: unset
Note: See TracTickets for help on using tickets.
Back to Top