I'm trying to call an external api from a class based view.
Currently I have the below view which calls the API. However at the moment I am just calling the API with api(username, password) and it returns the data but doesn't save it.
How can I also pass the model used in the modelform in the view to the api function so that it can save the returned data to the relevant user. (ie what method do I need to override in the CBV).
Additional what is the best method for calling an external api in django (to be initiated from a form completion). Currently the API round trip takes 5 -10 secs which at the moment the way it is setup delays the next page load by that amount.
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)
Model:
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)