Code

Opened 3 years ago

Last modified 13 months ago

#15522 new New feature

delete_model on admin delete action

Reported by: Divad Owned by: nobody
Component: contrib.admin Version: master
Severity: Normal Keywords: delete_model
Cc: shelldweller, omerd1 Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: yes
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.

Attachments (0)

Change History (12)

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

  • Component changed from Uncategorized to django.contrib.admin
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 3 years ago by julien

  • Resolution set to invalid
  • Status changed from new to closed

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

  • Resolution invalid deleted
  • Status changed from closed to reopened

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 3 years ago by julien

  • Triage Stage changed from Unreviewed to Accepted

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 3 years ago by julien

  • Needs documentation set
  • Needs tests set
  • Patch needs improvement set

comment:6 Changed 3 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 3 years ago by lrekucki

  • Severity set to Normal
  • Type set to New feature

comment:8 Changed 2 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:9 Changed 2 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:10 Changed 2 years ago by shelldweller

  • Cc shelldweller added

comment:11 Changed 2 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 13 months ago by aaugustin

  • Status changed from reopened to new

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.