I am working with a DRF serializer which includes a field with the PrimaryKeyRealtedField.
import stripe
from rest_framework import serializers
from subscriptions.models.subscription_product import SubscriptionProduct
class SubscriptionCheckoutSessionSerializer(serializers.Serializer):
url = serializers.CharField(read_only=True)
success_url = serializers.CharField(write_only=True, required=False)
cancel_url = serializers.CharField(write_only=True, required=False)
subscription_product = serializers.PrimaryKeyRelatedField(
queryset=SubscriptionProduct.objects.all()
)
def create(self, validated_data):
print(validated_data)
subscription_product = validated_data["subscription_product"]
print(subscription_product)
stripe_price = subscription_product.stripe_price
print(stripe_price)
print(stripe_price.stripe_id)
session = stripe.checkout.Session.create(
success_url=validated_data.get("success_url", None),
cancel_url=validated_data.get("cancel_url", None),
mode="subscription",
line_items=[
{
"price": stripe_price.stripe_id,
"quantity": 1,
}
],
)
print(session)
return session
The print statements all print their expected output and the created session is a valid one. However, I get a 500 status code with the following error:
KeyError: “Got KeyError when attempting to get a value for field subscription_product
on serializer SubscriptionCheckoutSessionSerializer
.nThe serializer field might be named incorrectly and not match any attribute or key on the Session
instance.nOriginal exception text was: ‘subscription_product’.”
Does anyone know what this error actually means and how to solve it? I don’t understand how it’s possible that the print statements work as expected but I still get a key error.