django异步如何改写这段代码?
使用djanog的异步如何改写下边这段代码:
class CategorysTagView(CommonView): """
根据一级分类名称获取所有二级分类
"""
def get(self, request, title):
# 根据名称获取一级分类
category = models.TopCategory.objects.prefetch_related('topcategory').get(title_en=title)
# 获取所有二级分类
dicts = {'category': category}
context = self.web_context(dicts)
return render(request, 'category.html', context)
回答:
from django.http import HttpResponsefrom asgiref.sync import sync_to_async
from . import models
class CategorysTagView(CommonView):
"""
根据一级分类名称获取所有二级分类
"""
async def get(self, request, title):
category = await sync_to_async(models.TopCategory.objects.prefetch_related('topcategory').get)(title_en=title)
dicts = {'category': category}
context = await sync_to_async(self.web_context)(dicts)
return HttpResponse(await sync_to_async(render)(request, 'category.html', context))
以上是 django异步如何改写这段代码? 的全部内容, 来源链接: utcz.com/p/938965.html