我正在使用Pinax-Stripe开发一个django应用程序。在我的模型中有这个代码:
class Plan(StripeObject):
amount = models.DecimalField(decimal_places=2, max_digits=9)
currency = models.CharField(max_length=15)
interval = models.CharField(max_length=15)
interval_count = models.IntegerField()
name = models.CharField(max_length=150)
statement_descriptor = models.TextField(blank=True)
trial_period_days = models.IntegerField(null=True)
def __str__(self):
return "{} ({}{})".format(self.name, CURRENCY_SYMBOLS.get(self.currency, ""), self.amount)
现在您知道在模型中,我们可以使用Model.objects.get(id=pk)
但是当我在这个'StripeObject'
中使用相同的内容时,它会给我一个未定义的错误。有谁知道如何从这个'Stripe Object'
模型中选择数据?
这是views.py的代码:
class SubscriptionCreateView(LoginRequiredMixin, PaymentsContextMixin, CustomerMixin, FormView):
template_name = "pinax/stripe_b/subscription_create.html"
form_class = PlanForm
def set_customer(self):
if self.customer is None:
self._customer = customers.create(self.request.user)
def subscribe(self, customer, plan, token):
subscriptions.create(customer, plan, token=token)
def form_valid(self, form):
self.set_customer()
try:
self.subscribe(self.customer, plan=form.cleaned_data["plan"], token=self.request.POST.get("stripeToken"))
#added code starts here
current_user = self.request.user
user_space = Space.objects.get(user = current_user)
user_space.available_harddrive = 1024
user_space.available_ram = 1024
user_space.save()
plan=form.cleaned_data["plan"]
plan_pk = Plan.get(id = plan.pk)
#added code ends here
return redirect("pinax_stripe_subscription_list")
except stripe.StripeError as e:
return self.render_to_response(self.get_context_data(form=form, errors=smart_str(e)))
这就是我遇到问题的地方:
plan=form.cleaned_data["plan"]
plan_pk = Plan.get(id = plan.pk)