I encountered a problem when I tried to use the async view
I have a model called modelA and a modelB, modelB has a foreign key relation to modelA
# models.py
class modelA(models.Model):
name = models.CharField("Full Name", max_length=48, unique=False, default="", validators=[validators.MinLengthValidator(2)])
class modelB(models.Model):
ma = models.ForeignKey(modelA, on_delete=models.CASCADE, verbose_name="ma")
Then I created a modelform, I want to render the foreign key as a select control in my template
# forms.py
class TestForm(forms.ModelForm):
class Meta:
model = modelB
fields = ["ma"]
widgets = {
"ma": forms.Select(attrs={"placeholder": "MA"})
}
I have an async view
# views.py
class TestView(View):
async def get(self, request, *args, **kwargs):
return render("template.html", {"form": "TestForm"})
And my template file
# template.html
<form method="post" action="">
{% csrf_token %}
{% for i in form %}
{{ i }}
{% endfor %}
</form>
When I tried to visit the page, Django showed an error SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.
, and the wrong code is {{ i }}
in template.
So my question is how can I render the foreign key in the template.