首页 > 解决方案 > 将新值从表单添加到 django 中的现有值

问题描述

我想将从表格中获得的数字加到模型中现有的数字中。我有一个带有整数字段的模型。我希望采取的任何新数据都应与模型中现有的数据相加。让 New-value + Old-value 然后保存答案。

我尝试使用更新而不是我最终替换了值而不是添加它们。但是我希望每当我的表单中出现新值时,它应该与选民字段中已经存在的值相加。

模型.py

   class Nomination(models.Model):
      Fullname = models.CharField(max_length=120)
      Nominee_ID = models.CharField(max_length=100)
      Category = models.ForeignKey(Category, on_delete=models.CASCADE)
      image = models.ImageField(upload_to='nominations_images')
      slug = models.SlugField(max_length=150)
      votes = models.IntegerField(default=0)
      date = models.DateTimeField(auto_now_add=True)

       def __str__(self):
        return self.Fullname

视图.py

def nomination_payView(request, slug):
if request.method == 'GET':
    model = get_object_or_404(Nomination, slug=slug)
    template_name = 'Payment.html'
    context = {
        'nomination': model
    }
    return render(request, 'Payment.html', context)
elif request.method == 'POST':
    url = 'https://test.testryr.net/v1.1/transaction/process' # change this when you go live
    encoded = base64.b64encode(b'd525bfd:ZDU5YTRiMG') # change this as well
    headers =  {
        'Content-Type': 'application/json',
        'Authorization': f'Basic {encoded.decode("utf-8")}',
        'Cache-Control': 'no-cache'
    }
    type_ = request.POST['type']
    amount = 0

    if type_ == 'card':

        card_name = request.POST['card_name']
        card_no = request.POST['card_no']
        email = request.POST['email']
        amount = str(request.POST['amount'] + '00')
        zeros = '0' * (12 - len(amount))
        print(zeros)
        str_amoount = zeros + amount
        exp_date = request.POST['exp_date'].split('/')
        cvv = request.POST['cvv']
        data = {
            "processing_code":"000000",
            "r-switch":"VIS",
            "transaction_id": random.randint(100000000000, 999999999999),
            "merchant_id": "TTM-00000740",
            "pan":card_no,
            "3d_url_response": f"http://127.0.0.1:8000/process-payment/{slug}/",
            "exp_month": exp_date[0],
            "exp_year": exp_date[1],
            "cvv": cvv,
            "desc":"Card Payment Test",
            "amount": str_amoount,
            "currency":"GHS",
            "card_holder": card_name,
            "customer_email": email
        }
        res = requests.post(url, data=json.dumps(data), headers=headers)
    elif type_ == 'mobile':
        network = request.POST['network']
        mobile_no = request.POST['mobile_no']
        amount = str(request.POST['amount'] + '00')
        zeros = '0' * (12 - len(amount))
        str_amoount = zeros + amount
        print(str_amoount)

        data = {
            "account_number": mobile_no,
            "account_issuer": network,
            "merchant_id": "TTM-00000740",
            "transaction_id": random.randint(100000000000, 999999999999),
            "processing_code":  "404000",
            "amount": str_amoount,
            "r-switch": network,
            "desc": "Float Transfer Test",
            "pass_code": "00000000000000000000000000000000" 
            }

        res = requests.post(url, data=json.dumps(data), headers=headers)

    if res.status_code == 200 and int(amount[:-2]) > 0:

        Nomination.objects.filter(slug=slug).update(votes+=amount[:-2])
        return redirect('/success')
    else:
       .... error page here ....
        return render(request, 'Payment.html')

标签: pythondjangodjango-modelsdjango-forms

解决方案


推荐阅读