Opened 14 years ago

Closed 7 years ago

#15522 closed New feature (fixed)

Allow customizing queryset deletion in the delete_selected admin action view

Reported by: Divad Owned by: nobody
Component: contrib.admin Version: dev
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

Description

Based on [Ticket #11108]

The correction should also include this change in actions.py:

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

To have the same result using delete_selected from actions.

Change History (18)

comment:1 by Divad <david@…>, 14 years ago

Component: Uncategorizeddjango.contrib.admin

comment:2 by Julien Phalip, 14 years ago

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 by Divad <david@…>, 14 years ago

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 by Julien Phalip, 14 years ago

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 by Julien Phalip, 14 years ago

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

comment:6 by Divad <david@…>, 14 years ago

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 by Łukasz Rekucki, 14 years ago

Severity: Normal
Type: New feature

comment:8 by Aymeric Augustin, 13 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:9 by Aymeric Augustin, 13 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:10 by Sergiy Kuzmenko, 13 years ago

Cc: Sergiy Kuzmenko added

comment:11 by omerd1, 13 years ago

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 by Aymeric Augustin, 12 years ago

Status: reopenednew

comment:13 by Paulo, 8 years ago

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 by Paulo, 8 years ago

Cc: commonzenpython@… added

comment:15 by Tim Graham, 8 years ago

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 by vangelou, 7 years ago

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.
https://github.com/django/django/pull/8990

comment:17 by vangelou, 7 years ago

Patch needs improvement: unset

comment:18 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: newclosed

In 777f216:

Fixed #15522 -- Added ModelAdmin.delete_queryset() to customize "delete selected objects" deletion.

Note: See TracTickets for help on using tickets.
Back to Top