Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin: override delete method

I have admin.py as follows:

  class profilesAdmin(admin.ModelAdmin):      list_display = ["type","username","domain_name"] 

Now i want to perform some action before deleting the object:

  class profilesAdmin(admin.ModelAdmin):      list_display = ["type","username","domain_name"]       @receiver(pre_delete, sender=profile)      def _profile_delete(sender, instance, **kwargs):         filename=object.profile_name+".xml"         os.remove(os.path.join(object.type,filename)) 

If i use delete signal method like this I get an error saying self should be the first parameter.

How can I modify the above function?
And I want to retrieve the profile_name of the object being deleted. How can this be done?

I also tried overriding delete_model method:

def delete_model(self, request, object):     filename=object.profile_name+".xml"     os.remove(os.path.join(object.type,filename))     object.delete() 

But this dosn't work if multiple objects have to be deleted at one shot.

like image 777
arjun Avatar asked Mar 04 '13 07:03

arjun


People also ask

How to delete a model in django admin?

You can use the delete_queryset which is coming from Django 2.1 onward for bulk delete objects and the delete_model for single delete. Both methods will handle something before deleting the object.

How do I delete a specific record in Django?

To delete a record we do not need a new template, but we need to make some changes to the members template. Of course, you can chose how you want to add a delete button, but in this example, we will add a "delete" link for each record in a new table column. The "delete" link will also contain the ID of each record.

What is soft delete in Django?

Soft Delete Model This means that Django will not create a database table for it. Now, we can create our models as subclasses of SoftDeleteModel to grant them the ability to be soft-deleted. Let's take the example of a Note model. class Note(SoftDeleteModel): user = models.


2 Answers

You can use the delete_queryset which is coming from Django 2.1 onward for bulk delete objects and the delete_model for single delete. Both methods will handle something before deleting the object.

ModelAdmin.delete_queryset(request, queryset)


This is the explanation about delete_queryset in release note of Django 2.1.

The delete_queryset() method is given the HttpRequest and a QuerySet of objects to be deleted. Override this method to customize the deletion process for the “delete selected objects”

Let's look at what delete_queryset does, you can override admin.ModelAdmin class in this way by including delete_queryset function. Here you'll get list of object(s), queryset.delete() mean delete all the object(s) at once or you can add a loop to delete one by one.

def delete_queryset(self, request, queryset):     print('==========================delete_queryset==========================')     print(queryset)      """     you can do anything here BEFORE deleting the object(s)     """      queryset.delete()      """     you can do anything here AFTER deleting the object(s)     """      print('==========================delete_queryset==========================') 

So I'm going to delete 5 objects from "select window" and here is those 5 objects.

deleting 5 objects from "select window"

Then you'll redirect to the confirmation page like this,

going to delete those 5 objects

Keep it mind about "Yes, I'm sure" button and I'll explain it later. When you click that button you will see the below image after removing those 5 objects.

successfully deleted 5 objects

This is the terminal output,

terminal output of those 5 objects

So you'll get those 5 objects as a list of QuerySet and before deleting you can do anything what ever you want in the comment area.

ModelAdmin.delete_model(request, obj)


This is the explanation about delete_model.

The delete_model method is given the HttpRequest and a model instance. Overriding this method allows doing pre- or post-delete operations. Call super().delete_model() to delete the object using Model.delete().

Let's look at what delete_model does, you can override admin.ModelAdmin class in this way by including delete_model function.

actions = ['delete_model']  def delete_model(self, request, obj):     print('============================delete_model============================')     print(obj)      """     you can do anything here BEFORE deleting the object     """      obj.delete()      """     you can do anything here AFTER deleting the object     """      print('============================delete_model============================') 

I just click my 6th object to delete from the "change window".

deleting 1 object from "change window"

There is another Delete button, when you click it you'll see the window which we saw earlier.

going to delete those 1 object

Click "Yes, I'm sure" button to delete the single object. You'll see the following window with the notification of that deleted object.

successfully deleted 1 object

This is the terminal output,

terminal output of those 1 object

So you'll get selected object as a single of QuerySet and before deleting you can do anything what ever you want in the comment area.


The final conclusion is you can handle the delete event by clicking "Yes, I'm sure" button in "select window" or "change window" in Django Admin Site using delete_queryset and delete_model. In this way we don't need to handle such a signals like django.db.models.signals.pre_delete or django.db.models.signals.post_delete.

Here is the full code,

from django.contrib import admin  from . import models  class AdminInfo(admin.ModelAdmin):     model = models.AdminInfo     actions = ['delete_model']      def delete_queryset(self, request, queryset):         print('========================delete_queryset========================')         print(queryset)          """         you can do anything here BEFORE deleting the object(s)         """          queryset.delete()          """         you can do anything here AFTER deleting the object(s)         """          print('========================delete_queryset========================')      def delete_model(self, request, obj):         print('==========================delete_model==========================')         print(obj)          """         you can do anything here BEFORE deleting the object         """          obj.delete()          """         you can do anything here AFTER deleting the object         """          print('==========================delete_model==========================')  admin.site.register(models.AdminInfo, AdminInfo) 
like image 58
Kushan Gunasekera Avatar answered Sep 20 '22 23:09

Kushan Gunasekera


You're on the right track with your delete_model method. When the django admin performs an action on multiple objects at once it uses the update function. However, as you see in the docs these actions are performed at the database level only using SQL.

You need to add your delete_model method in as a custom action in the django admin.

def delete_model(modeladmin, request, queryset):     for obj in queryset:         filename=obj.profile_name+".xml"         os.remove(os.path.join(obj.type,filename))         obj.delete() 

Then add your function to your modeladmin -

class profilesAdmin(admin.ModelAdmin):     list_display = ["type","username","domain_name"]     actions = [delete_model] 
like image 21
Aidan Ewen Avatar answered Sep 20 '22 23:09

Aidan Ewen