How can I render foreign key field in template with async view

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.

In Django, templates are processed in a synchronous context by default. If you have an asynchronous view (using async def ), you cannot directly render the form in the template using the synchronous template engine.
To handle this situation, you can use sync_to_async as suggested in the error to convert synchronous functions to asynchronous ones.

1 Like

Thanks for your reply, I use the sync_to_async function to change the render function to the async function, and it works