在使用django.db.models.signals.post_save

时间:2018-08-25 00:12:16

标签: security django-signals django-request

我正在尝试跟踪数据库中的更改并将其链接到IP地址。我正在request函数中传递get_ip,并用alert_changepost_save.connect()中调用该函数。我收到错误消息“ alert_request缺少一个必需的参数:'request'”。似乎在使用post_save信号时无法传递其他参数。实现我在同一功能中使用信号和请求的目标的最佳方法是什么?谢谢。

def get_ip(request):
ip, is_routable = get_client_ip(request)
if ip is None:
    return "unable to get IP address"
else:
    print(ip)
    if is_routable:
        return "public IP"
    else:
        return "Private IP Address"


def alert_change(sender, instance, created, request, **kwarg):
    if created:
        print("Account created for {}.".format(instance.first_name))
        print(get_ip(request))
    else:
        print("change for {}{}. Credit: {}".format(instance.first_name, instance.last_name, instance.credit))


post_save.connect(alert_change, sender=Customer)

0 个答案:

没有答案