我试图从基于类的视图调用外部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
好的,更新了一个答案。 –