首页 > 解决方案 > 条带获取订阅数据(错误:关系“subscriptions_stripecustomer”不存在)

问题描述

我收到这个错误。

关系“subscriptions_stripecustomer”不存在

我正在尝试按照本手册https://testdriven.io/blog/django-stripe-subscriptions/配置 Django Stripe 订阅

下面的代码是获取订阅数据的views.py。它遵循上述手册。

import stripe
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User  
from django.http.response import JsonResponse, HttpResponse  
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt

from subscriptions.models import StripeCustomer  

@login_required
def home(request):
    try:
        # Retrieve the subscription & product
        stripe_customer = StripeCustomer.objects.get(user=request.user)
        stripe.api_key = settings.STRIPE_SECRET_KEY
        subscription = stripe.Subscription.retrieve(stripe_customer.stripeSubscriptionId)
        product = stripe.Product.retrieve(subscription.plan.product)

        # Feel free to fetch any additional data from 'subscription' or 'product'
        # https://stripe.com/docs/api/subscriptions/object
        # https://stripe.com/docs/api/products/object

        return render(request, 'home.html', {
            'subscription': subscription,
            'product': product,
        })

    except StripeCustomer.DoesNotExist:
        return render(request, 'home.html')
    
@csrf_exempt
def stripe_webhook(request):
print("webhook page is opend")
stripe.api_key = settings.STRIPE_SECRET_KEY
endpoint_secret = settings.STRIPE_ENDPOINT_SECRET
payload = request.body
sig_header = request.META['HTTP_STRIPE_SIGNATURE']
event = None


try:
    event = stripe.Webhook.construct_event(
        payload, sig_header, endpoint_secret
    )
except ValueError as e:
    # Invalid payload
    return HttpResponse(status=400)
except stripe.error.SignatureVerificationError as e:
    # Invalid signature
    return HttpResponse(status=400)

# Handle the checkout.session.completed event
if event['type'] == 'checkout.session.completed':
    session = event['data']['object']

    # Fetch all the required data from session
    client_reference_id = session.get('client_reference_id')
    stripe_customer_id = session.get('customer')
    stripe_subscription_id = session.get('subscription')

    # Get the user and create a new StripeCustomer
    user = User.objects.get(id=client_reference_id)
    StripeCustomer.objects.create(
        user=user,
        stripeCustomerId=stripe_customer_id,
        stripeSubscriptionId=stripe_subscription_id,
    )
    print(user.username + ' just subscribed.')

return HttpResponse(status=200)

错误可能发生在

stripe_customer = StripeCustomer.objects.get(user=request.user)

或者

用户 = User.objects.get(id=client_reference_id)

因为我使用自定义用户模型“CustomUser”

我的模型.py

from django.conf import settings
from django.db import models


class StripeCustomer(models.Model):
    user = models.OneToOneField(to=settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    stripeCustomerId = models.CharField(max_length=255)
    stripeSubscriptionId = models.CharField(max_length=255)

    def __str__(self):
        return self.user.username

帐户/模型.py

from django.contrib.auth.models import AbstractUser


class CustomUser(AbstractUser):

    class Meta:
        verbose_name_plural = 'CustomUser'

我的设置.py

#used for django-allauth
AUTH_USER_MODEL = 'accounts.CustomUser'

在这种情况下,我应该如何正确更改以下代码以及自定义用户模型“CustomUser”?

stripe_customer = StripeCustomer.objects.get(user=request.user)

或者

用户 = User.objects.get(id=client_reference_id)

或添加一些其他代码?

我刚刚在这个问题中提到了上述设置,但如果需要更多代码,然后告诉我我会用这些信息更新我的问题。谢谢

标签: pythondjangodjango-modelsdjango-rest-frameworkstripe-payments

解决方案


推荐阅读