Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 条带获取订阅数据(错误:关系“subscriptions”和“stripecustomer”不存在)_Python_Django_Django Models_Django Rest Framework_Stripe Payments - Fatal编程技术网

Python 条带获取订阅数据(错误:关系“subscriptions”和“stripecustomer”不存在)

Python 条带获取订阅数据(错误:关系“subscriptions”和“stripecustomer”不存在),python,django,django-models,django-rest-framework,stripe-payments,Python,Django,Django Models,Django Rest Framework,Stripe Payments,我得到了这个错误 关系“subscriptions\u stripecustomer”不存在 我正试图按照本手册配置Django条带订阅 下面的代码是views.py,它获取订阅数据。 它遵循上面的手册 import stripe from django.conf import settings from django.contrib.auth.decorators import login_required from django.contrib.auth.models import User

我得到了这个错误

关系“subscriptions\u stripecustomer”不存在

我正试图按照本手册配置Django条带订阅

下面的代码是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\u customer=StripeCustomer.objects.get(user=request.user)

user=user.objects.get(id=client\u reference\u id)

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

我的模特

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
accounts/models.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
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\u customer=StripeCustomer.objects.get(user=request.user)

user=user.objects.get(id=client\u reference\u id)

或者添加一些其他代码


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

您是否执行了“最终运行
migrate
以同步数据库和runserver以启动Django的本地web服务器”这篇博文中的步骤?这里的错误听起来好像数据库没有初始化。@karllekko是的,我初始化了。