Django IntegrityError UNIQUE 约束失败:plans_customer.user_id

问题描述

我的结帐视图有问题。通过电子邮件添加用户验证后,出现完整性错误。我想知道如何解决这个问题。我已经从互联网上查看了一些解决方案,但没有一个对我有用。

一些基本信息:

请求方式:POST

请求网址:http://127.0.0.1:8000/checkout

Django 版本:2.2.3

异常类型:IntegrityError

异常值:

UNIQUE 约束失败:plans_customer.user_id

这是我的结帐视图:

@login_required

定义结帐(请求):

try:
    if request.user.customer.membership:
        return redirect('settings')
except Customer.DoesNotExist:
    pass

coupons = {'bank':25,'city':25}

if request.method == 'POST':
    stripe_customer = stripe.Customer.create(email=request.user.email,source=request.POST['stripeToken'])
    plan = 'price_1IC3TwL0fAQS9DO5lqZSmcew'
    if request.POST['plan'] == 'yearly':
        plan = 'price_1IC3TwL0fAQS9DO5IKzHz5ZI'
    if request.POST['coupon'] in coupons:
        percentage = coupons[request.POST['coupon'].lower()]
        try:
            coupon = stripe.Coupon.create(duration='once',id=request.POST['coupon'].lower(),percent_off=percentage)
        except:
            pass
        subscription = stripe.Subscription.create(customer=stripe_customer.id,items=[{'plan':plan}],coupon=request.POST['coupon'].lower())
    else:
        subscription = stripe.Subscription.create(customer=stripe_customer.id,items=[{'plan':plan}])

    customer = Customer()
    customer.user = request.user
    customer.stripeid = stripe_customer.id
    customer.membership = True
    customer.cancel_at_period_end = False
    customer.stripe_subscription_id = subscription.id
    customer.save()

    msg_plain = render_to_string('mails/email.txt')
    msg_html = render_to_string('mails/email.html')
    subject = 'Welcome to J & M Traders'
    recepient = str(request.user.email)
    send_mail(subject,msg_plain,EMAIL_HOST_USER,[recepient],html_message=msg_html)

    return redirect('home')
else:
    coupon = 'none'
    plan = 'monthly'
    price = 9999
    og_dollar = 99.99
    coupon_dollar = 0
    final_dollar = 99.99
    if request.method == 'GET' and 'plan' in request.GET:
        if request.GET['plan'] == 'yearly':
            plan = 'yearly'
            price = 94999
            og_dollar = 949.99
            final_dollar = 949.99
    if request.method == 'GET' and 'coupon' in request.GET:
        print(coupons)
        if request.GET['coupon'].lower() in coupons:
            print('fam')
            coupon = request.GET['coupon'].lower()
            percentage = coupons[request.GET['coupon'].lower()]


            coupon_price = int((percentage / 100) * price)
            price = price - coupon_price
            coupon_dollar = str(coupon_price)[:-2] + '.' + str(coupon_price)[-2:]
            final_dollar = str(price)[:-2] + '.' + str(price)[-2:]


    return render(request,'plans/checkout.html',{'plan':plan,'coupon':coupon,'price':price,'og_dollar':og_dollar,'coupon_dollar':coupon_dollar,'final_dollar':final_dollar})

和我的模型:

class Customer(models.Model):
   user = models.OneToOneField(User,on_delete=models.CASCADE)
   email_confirmed = models.BooleanField(default=False)
   stripeid = models.CharField(default=False,max_length=255)
   stripe_subscription_id = models.CharField(default=False,max_length=255)
   cancel_at_period_end = models.BooleanField(default=False)
   membership = models.BooleanField(default=False)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...