You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django对接Stripe API保存支付模型时card_exp_month字段类型不匹配错误排查求助

Fix: Field 'card_exp_month' expected a number but got (4,)

一眼就能揪出问题啦——你在给Payment模型字段赋值的时候,每一行末尾都多写了一个逗号!

看你代码里这些行:

payment.card_brand = str(charge.payment_method_details.card.brand),
payment.card_exp_year = int(charge.payment_method_details.card.exp_year),
payment.card_exp_month = int(charge.payment_method_details.card.exp_month),
payment.card_last4 = str(charge.payment_method_details.card.last4),

在Python里,单个值后面加逗号会把它变成一个单元素元组,所以原本的数字4就被包装成了(4,),而你的模型里card_exp_month是SmallIntegerField,只接受整数类型,自然就抛出类型不匹配的错误了。

解决方案

把这些赋值行末尾的逗号全部删掉就行,修改后的代码如下:

def process_card(order, charge):
    print("I HAVE THE DATA AS: ", charge)
    print("I have the brand as: ", charge.payment_method_details.card.brand)
    print("I have the expiry year as: ", charge.payment_method_details.card.exp_year)
    print("I have the expiry month as: ", charge.payment_method_details.card.exp_month)
    print("I have the last 4 as: ", charge.payment_method_details.card.last4)
    payment = order.payment
    payment.stripe_payment_intent = order.stripe_payment_intent
    payment.total_paid = Money(charge.amount / 100, charge.currency)
    payment.card_brand = str(charge.payment_method_details.card.brand)
    payment.card_exp_year = int(charge.payment_method_details.card.exp_year)
    payment.card_exp_month = int(charge.payment_method_details.card.exp_month)
    payment.card_last4 = str(charge.payment_method_details.card.last4)
    payment.receipt_url = charge.receipt_url
    payment.save()
    order.stripe_payment_intent = None
    order.order_flow = "PAID"
    order.save()

验证逻辑

你控制台打印出来的4是正确的原始值,但赋值时因为多了逗号,被转换成了元组(4,),这就是错误信息的来源。删掉逗号后,赋值的就是纯整数类型,和模型字段要求完全匹配,问题就能解决。

内容的提问来源于stack exchange,提问作者Darren

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 12:53:10