Django:使用 post_delete 信号时确定正在删除的用户

发布于 2024-11-26 21:32:08 字数 765 浏览 0 评论 0原文

我希望在删除某些对象时通知管理员,但我也想确定哪个用户正在执行删除。

是否可以?

这是代码:

# models.py
# signal to notify admins when nodes are deleted
from django.db.models.signals import post_delete
from settings import DEBUG

def notify_on_delete(sender, instance, using, **kwargs):
    ''' Notify admins when nodes are deleted. Only for production use '''
    if DEBUG:
        #return False
        pass
    # prepare context
    context = {
        'node': instance,
        'site': SITE
    }
    # notify admins that want to receive notifications
    notify_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)

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 DEBUG

def notify_on_delete(sender, instance, using, **kwargs):
    ''' Notify admins when nodes are deleted. Only for production use '''
    if DEBUG:
        #return False
        pass
    # prepare context
    context = {
        'node': instance,
        'site': SITE
    }
    # notify admins that want to receive notifications
    notify_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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吾性傲以野 2024-12-03 21:32:08

我怀疑是否可以使用内置信号(没有 User 隐式绑定到 delete 操作,并且由于 Django 的 松散耦合数据库层不处理HttpRequest 对象)。我将创建自己的信号,它提供 user 参数,并将其发送到发生删除操作的任何视图中,例如:

# 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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文