django you cannot call this from an async context -use a thread or sync_to_async

django在异步的代码中调用ORM,报错you cannot call this from an async context -use a thread or sync_to_async

解决方案:
如果是django3以及以上,可以考虑尝试如下代码:

from channels.db import database_sync_to_async

@database_sync_to_async
def your_ORM_function():
	data = models.SomeModel.objects.get()
	# 对data进行操作
	data.save()
	# 对data保存

你可能感兴趣的:(django,python,django,python,后端)