Django保存多个视图变量

时间:2018-01-12 15:49:55

标签: python django webhooks

我正在尝试将网址参数保存到我的模型中。已经尝试了很多东西,目前正在争论错误。找不到合适的文档,因为我不确定正确的搜索词是什么。

我需要做的就是将webhook POST保存到我的URL。我将使用@require_POST装饰器来仅要求POST。 Atm这不是问题。问题只是保存webhook。

models.py

from django.db import models
from django.contrib.auth.models import User

class Webhook(models.Model):
    clientAccnum = models.CharField(max_length=120, blank=True)
    clientSubacc = models.CharField(max_length=120, blank=True)
    eventType = models.CharField(max_length=120, blank=True)
    eventGroupType = models.CharField(max_length=120, blank=True)
    subscriptionId = models.CharField(max_length=120, blank=True)
    time_stamp = models.DateTimeField(blank=True)
    time_stamp_local = models.DateTimeField(blank=True)

views.py

def webhook(request):
    template_name = 'payment/index.html'

    hook = Webhook.save()
    hook.client_acc_num = request.GET.get('clientAccnum')
    hook.client_sub_acc = request.GET.get('clientSubacc')
    hook.event_type = request.GET.get('eventType')
    hook.event_group_type = request.GET.get('eventGroupType')
    hook.sub_id = request.GET.get('subscriptionId')

    hook.time_stamp = request.GET.get('timestamp')
    hook.time_stamp_local = timezone.now()

    hook.save()

    return render(request, template_name)

当前错误消息:

/ payment / webhook /上的TypeError save()缺少1个必需的位置参数:'self'

1 个答案:

答案 0 :(得分:1)

您无法直接在课堂上致电.save()。它是一个实例方法,因此需要在实例上调用它。

我不知道你为什么要在开始时打电话保存。你最后调用它来保存你设置的数据。

仅用hook = Webhook()替换第二行。