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.
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.
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.
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.
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.
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.
Then you'll redirect to the confirmation page like this,
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.
This is the terminal output,
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.
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".
There is another Delete button, when you click it you'll see the window which we saw earlier.
Click "Yes, I'm sure" button to delete the single object. You'll see the following window with the notification of that deleted object.
This is the terminal output,
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)
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With