Hello all,
I was wondering what the best way is when using forms in an async context. For example:
class View(FormView):
async def post(self, request, *args, **kwargs):
form = self.get_form()
kwargs.update({
'form': form,
})
if form.is_valid():
return await self.form_valid(**kwargs)
else:
return await self.form_invalid(**kwargs)
async def form_valid(self, *kwargs):
form = kwargs.get('form')
email = form.cleaned_data('email')
# value: <coroutine object SyncToAsync.__call__ at 0x7f3530debe20>
# do stuff
and the simple form:
class Form():
email = forms.EmailField(
required=True,
label='Email',
initial='',
max_length=255,
widget=forms.EmailInput(
attrs={
'class': 'fc',
'placeholder': 'Enter your email',
'spellcheck': 'false',
'autocorrect': 'off',
'autofocus': '',
}
)
)
@sync_to_async
def clean_email(self):
email = self.cleaned_data.get('email', None)
try:
User.objects.only('id').get(email=email)
raise forms.ValidationError(f'There was a problem creating your account. Please try again')
except User.DoesNotExist:
pass
return email
def clean(self):
cleaned_data = self.cleaned_data
return cleaned_data
again, the value returned is <coroutine object SyncToAsync.__call__ at 0x7f3530deb1c0>
and not a string
What am I doing wrong?
. forgot to “async” some other methods in the form validation chain?
. something else?
Thanks