Django save()方法不将数据插入数据库

时间:2015-09-02 09:26:13

标签: python mysql django

使用Django models.save()方法将数据插入数据库时​​,我遇到了一个奇怪的问题。我一直在同一项目的其他一些应用程序中使用save()方法。在这个名为payment的应用程序中,我收到了来自支付网关的post请求,并且我在成功时将数据插入数据库

我的付款模式的结构如下:

付款方式

from django.db import models

# Create your models here.
class payments(models.Model):
    transactionId = models.CharField(max_length=50,default=None)
    uniqueOrderId = models.CharField(max_length=50,default=None)
    userId = models.CharField(max_length=30,default=None)
    productId = models.CharField(max_length=30,default=None)
    amount = models.CharField(max_length=30,default=None)
    date = models.CharField(max_length=30,default=None)

Views.py

from django.shortcuts import render, render_to_response
from django.http import HttpResponse,HttpResponseRedirect
from django.template.loader import get_template
from django.template import Context, Template,RequestContext
import datetime
import hashlib
from random import randint
from django.views.decorators.csrf import csrf_protect, csrf_exempt
from django.core.context_processors import csrf
from django.contrib.auth.models import User
from questions.models import questions as questionData
from payments.models import payments

def success(request):
    c = {}
    c.update(csrf(request))
    status=request.POST["status"]
    firstname=request.POST["firstname"]
    amount=request.POST["amount"]
    txnid=request.POST["txnid"]
    posted_hash=request.POST["hash"]
    key=request.POST["key"]
    productinfo=request.POST["productinfo"]
    email=request.POST["email"]
    uniqueOrder = request.POST["paymentGatewayOrderID"]
    useridReturn = request.POST["udf1"]
    productIdReturn = request.POST["udf2"]
    salt="mysecretsalt"
    try:
        additionalCharges=request.POST["additionalCharges"]
        retHashSeq=additionalCharges+'|'+salt+'|'+status+'|||||||||||'+email+'|'+firstname+'|'+productinfo+'|'+amount+'|'+txnid+'|'+key
    except Exception:
        retHashSeq = salt+'|'+status+'|||||||||||'+email+'|'+firstname+'|'+productinfo+'|'+amount+'|'+txnid+'|'+key
    hashh=hashlib.sha512(retHashSeq).hexdigest().lower()
    if(hashh !=posted_hash):
        print "Invalid Transaction. Please try again"
    else:
        paymentDataInsert = payments(transactionId=txnid,userId=useridReturn,productId=productIdReturn,amount=amount,uniqueOrderId=uniqueOrder,date="today")
        paymentDataInsert.save()
        print "Thank You. Your order status is ", status
        print "Your Transaction ID for this transaction is ",txnid
        print "We have received a payment of Rs. ", amount ,". Your order will soon be shipped."
        return render_to_response('success.html',RequestContext(request,{"txnid":txnid,"status":status,"amount":amount,"useridReturn":useridReturn,"uniqueOrder":uniqueOrder,"productIdReturn":productIdReturn}))

付款正确处理,所有数据都在success.html模板上正确呈现。

但即使我正确调用

,数据也不会插入到表中
paymentDataInsert = payments(transactionId=txnid,userId=useridReturn,productId=productIdReturn,amount=amount,uniqueOrderId=uniqueOrder,date="today")
paymentDataInsert.save()

奇怪的是,它没有显示任何错误消息。我尽力解决问题,但没有成功。

修改1

我尝试使用其他视图中的另一个函数paymentDataInsert.save(),其中包含条目的默认值。然后它奏效了。似乎问题仅适用于该特定功能。

0 个答案:

没有答案