首页 > 解决方案 > 如何在 post_save 函数中获取请求

问题描述

您好,我正在使用 Post_save 信号从模型发送通知....

但我收到这样的错误“sending_notification() 缺少 1 个必需的位置参数:'request'”

@receiver(post_save, sender=Plan)
def sending_notification(request, sender, instance,created, **kwargs):
    if created:
        notify.send(request.user, recipient = request.user, verb = "Some messages")

标签: djangodjango-modelsdjango-signals

解决方案


您不能在那里访问请求,从信号参数中删除请求。

@receiver(post_save, sender=Plan)
def sending_notification(sender, instance, created, **kwargs):
    if created:
        pass

我在这里看到了您要实现的目标,您正在使用 django-notifications-hq 向用户发送通知,您可以在视图本身中执行此操作,因为请求在模型信号中不可用。

https://stackoverflow.com/a/4716440/8105570


推荐阅读