【问题标题】:How to get a request in post_save function如何在 post_save 函数中获取请求
【发布时间】:2020-11-30 08:15:12
【问题描述】:

您好,我正在使用 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")

【问题讨论】:

    标签: django django-models django-signals


    【解决方案1】:

    你不能在那里访问request,请从信号参数中删除request

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-10
      • 1970-01-01
      • 2021-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-21
      • 2021-01-12
      相关资源
      最近更新 更多