Python Django发送API请求最佳实践:内部模型保存还是序列化程序?
我有一个名为Python Django发送API请求最佳实践:内部模型保存还是序列化程序?,python,django,stripe-payments,Python,Django,Stripe Payments,我有一个名为Customer的模型,在模型保存方法中,每当在我的应用程序上创建客户实例时,我都会向条带API发送一个创建客户的请求。另一种方法是在serializer.py文件中。也就是说,当用户在我的应用程序上创建客户实例时,它将自动向stripe发送请求,以在该端创建客户。以下哪项是更好的做法?我相信,为了“更新”客户,我必须将请求从serializers.py文件发送到stripe。因此,也可以从该文件创建客户。任何帮助都将不胜感激。您的两种方法都是正确的,无论是在保存方法中请求条带,还是
Customer
的模型,在模型保存方法中,每当在我的应用程序上创建客户实例时,我都会向条带API发送一个创建客户的请求。另一种方法是在serializer.py文件中。也就是说,当用户在我的应用程序上创建客户实例时,它将自动向stripe发送请求,以在该端创建客户。以下哪项是更好的做法?我相信,为了“更新”客户,我必须将请求从serializers.py文件发送到stripe。因此,也可以从该文件创建客户。任何帮助都将不胜感激。您的两种方法都是正确的,无论是在保存方法中请求条带,还是在用户创建客户之后。您不需要序列化程序来更新客户。简单地说,你们可以做到这一点,只是一个更新卡的例子
def update_card(request):
if request.method == 'POST':
"""
replace old card with new
"""
customer = Customer.objects.get(user=request.user).stripe_cust_id
# Customer is my custom table that recored stripe customer information
stripe.Customer.modify(
customer,
source=request.POST['stripeToken'],
)
我的模板
<form action="{% url 'subscriptions:update_card' %}" method="post" id="payment-form">{% csrf_token %}
<div class="card-input-wrap m-4">
<label for="card-element">Credit or debit card</label>
<div id="card-element">
<!-- a Stripe Element will be inserted here. -->
</div>
<!-- Used to display form errors -->
<div id="card-errors" role="alert"></div>
</div>
<button type="submit" class="btn btn-primary ml-4 mb-4">Save Info <i class="la la-save"></i></button>
</form>
{%csrf\u令牌%}
信用卡还是借记卡
保存信息
无论是在保存方法中请求条带,还是在用户创建客户之后,这两种方法都是正确的。您不需要序列化程序来更新客户。简单地说,你们可以做到这一点,只是一个更新卡的例子
def update_card(request):
if request.method == 'POST':
"""
replace old card with new
"""
customer = Customer.objects.get(user=request.user).stripe_cust_id
# Customer is my custom table that recored stripe customer information
stripe.Customer.modify(
customer,
source=request.POST['stripeToken'],
)
我的模板
<form action="{% url 'subscriptions:update_card' %}" method="post" id="payment-form">{% csrf_token %}
<div class="card-input-wrap m-4">
<label for="card-element">Credit or debit card</label>
<div id="card-element">
<!-- a Stripe Element will be inserted here. -->
</div>
<!-- Used to display form errors -->
<div id="card-errors" role="alert"></div>
</div>
<button type="submit" class="btn btn-primary ml-4 mb-4">Save Info <i class="la la-save"></i></button>
</form>
{%csrf\u令牌%}
信用卡还是借记卡
保存信息