Django: determine which user is deleting when using post_delete signal

穿精又带淫゛_ 提交于 2019-12-04 06:51:24

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!