Django: determine which user is deleting when using post_delete signal

2024/10/10 10:28:28

I want admins to be notified when certain objects are deleted but I also want to determine which user is performing the delete.

Is it possible?

This is the code:

# models.py
# signal to notify admins when nodes are deleted
from django.db.models.signals import post_delete
from settings import DEBUGdef notify_on_delete(sender, instance, using, **kwargs):''' Notify admins when nodes are deleted. Only for production use '''if DEBUG:#return Falsepass# prepare contextcontext = {'node': instance,'site': SITE}# notify admins that want to receive notificationsnotify_admins(instance, 'email_notifications/node-deleted-admin_subject.txt', 'email_notifications/node-deleted-admin_body.txt', context, skip=False)post_delete.connect(notify_on_delete, sender=Node)
Answer

I doubt it's possible using the built-in signals (there is no User implicitly tied to a delete operation, and because of Django's loose coupling the database layer doesn't deal with HttpRequest objects). I would create my own signal which provides a user argument and send it in whatever view the delete operation takes place, something like:

# myapp/signals.py
from django.dispatch import Signal
my_post_delete = Signal(providing_args=['instance', 'user'])# myapp/models.py
from myapp.signals import my_post_delete
...
my_post_delete.connect(notify_on_delete, sender=Node)# myapp/views.py
from myapp.signals import my_post_delete
...
@login_required
def my_delete_view(request, ...)...instance = Node.objects.get(...)instance.delete()my_post_delete.send(sender=Node, instance=instance, user=request.user)
https://en.xdnf.cn/q/69910.html

Related Q&A

Double inheritance causes metaclass conflict

I use two django packages - django-mptt (utilities for implementing Modified Preorder Tree Traversal) and django-hvad (model translation).I have a model class MenuItem and I want to it extends Translat…

Mask area outside of imported shapefile (basemap/matplotlib)

Im plotting data on a basemap of the eastern seaboard of the U. S. and Canada through Matplotlib. In addition to the base layer (a filled contour plot), I overlayed a shapefile of this focus region ato…

Python Glob.glob: a wildcard for the number of directories between the root and the destination

Okay Im having trouble not only with the problem itself but even with trying to explain my question. I have a directory tree consisting of about 7 iterations, so: rootdir/a/b/c/d/e/f/destinationdirThe …

Get datetime format from string python

In Python there are multiple DateTime parsers which can parse a date string automatically without providing the datetime format. My problem is that I dont need to cast the datetime, I only need the dat…

Generating an optimal binary search tree (Cormen)

Im reading Cormen et al., Introduction to Algorithms (3rd ed.) (PDF), section 15.4 on optimal binary search trees, but am having some trouble implementing the pseudocode for the optimal_bst function in…

Pydub from_mp3 gives [Errno 2] No such file or directory

I find myself in front of a wall here, simply trying to load an audio file into pydub for converting it keeps on throwing a "[Errno 2] No such file or directory" error.Naturally I have spent …

Compile Python 3.6.2 on Debian Jessie segfaults on sharedmods

Im trying to compile Python 3.6.2 on a Debian Jessie box with the options./configure --prefix="/opt/python3" \ --enable-optimizations \--with-lto \ --enable-profiling \ --enable-unicode=ucs4 …

Escape space in filepath

Im trying to write a python tool that will read a logfile and process itOne thing it should do is use the paths listed in the logfile (its a logfile for a backup tool)/Volumes/Live_Jobs/Live_Jobs/*SCAN…

How to save web page as text file?

I would like to save a web page (all content) as a text file. (As if you did right click on webpage -> "Save Page As" -> "Save as text file" and not as html file)I have tried …

PIL Image.size returns the opposite width/height

Using PIL to determine width and height of imagesOn a specific image (luckily only this one - but it is troubling) the width/height returning from image.size is the opposite. The image: http://storage.…