首页 > 解决方案 > AttributeError:“元组”对象没有属性“stripe_customer_id”

问题描述

尝试在 django 应用程序中使用 stripe api 提交购买时,我收到以下错误。

第 115 行,如果 userprofile.stripe_customer_id != '' 并且 userprofile.stripe_customer_id 不是 None:AttributeError: 'tuple' object has no attribute 'stripe_customer_id' [09/Oct/2019 19:18:26] "POST /api/结帐/HTTP/1.1" 500 16291

一切正常,直到我根据22:33进行更改。

这是第 115 行:

 if userprofile.stripe_customer_id != '' and userprofile.stripe_customer_id is not None:
            customer = stripe.Customer.retrieve(
                userprofile.stripe_customer_id)
            customer.sources.create(source=token)

标签: djangostripe-payments

解决方案


您在这里发布了太多代码。

问题在这里:

userprofile = UserProfile.objects.get_or_create(user=self.request.user)

get_or_create返回一个元组:(object, created). 您已将整个元组分配给userprofile变量。

由于您不关心created,请将其分配给一次性名称:

userprofile, _ = UserProfile.objects.get_or_create(user=self.request.user)

推荐阅读