我試圖從基於類的視圖調用外部api。 目前我有下面哪個調用API的視圖。然而目前我只是用api(username, password)
調用API,它返回數據但不保存。django從視圖調用外部api
我該如何將視圖中modelform中使用的模型傳遞給api函數,以便它可以將返回的數據保存到相關用戶。 (即我需要在CBV中重寫什麼方法)。
另外什麼是在Django中調用外部API的最佳方法(從表單完成啓動)。目前,API往返時間需要5-10秒,目前它的設置方式會延遲下一頁加載量。
class SupplierOnlineAccountView(CreateView):
form_class = SupplierOnlineAccountForm
template_name = 'standard_form.html'
success_url = '../contacting'
def form_valid(self, form):
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
api(username, password)
return super().form_valid(form)
型號:
class EUser(models.Model):
username = models.CharField(max_length=255, null=True)
password = models.CharField(max_length=255, null=True)
address = models.ForeignKey(Address, null=True)
temp_user = models.CharField(max_length=255, null=True)
user = models.OneToOneField(settings.AUTH_USER_MODEL, null=True, default=None)
title = models.CharField(max_length=10)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
你能詳細說明你到底想做什麼:你得到的數據,你想要保存的數據 –
當然,謝謝,我試圖將返回的API數據保存在Euser模型中。 SupplierOnlineAccountForm也保存到Euser模型。 Api返回first_name,last_name等...和其他數據以保存在EUser模型中。 – Yunti
好的,更新了一個答案。 –