我无法使用 Tornado 处理程序保存 MotorEngine 实例。以下是我为简洁起见缩短的代码摘录。
如果我用实例替换motorengine
导入会正确保存。mongoengine
# model
from motorengine.document import Document
from motorengine.fields import *
class Test(Document):
nameTest = StringField(required=True)
numberTest = DecimalField(required=True)
boolTest = BooleanField(required=True)
# handler
class TestHandler(BaseHandler):
@tornado.gen.coroutine
def post(self, *args, **kwargs):
response = self._service.save_test()
print(response)
self.write('')
self.finish()
# save method
from models import Test
def save_test(self):
yield Test.objects.create(nameTest="foobar", boolTest=False, numberTest=123)
我的代码中是否有任何明显的错误?异步编程不是我的强项。
:编辑:
根据本的回答,这是有效的代码
# handler
@tornado.gen.coroutine
def post(self, *args, **kwargs):
yield self._service.save_test()
# save method
@tornado.gen.coroutine
def save_test(self):
yield Test.objects.create(nameTest="foobar", boolTest=False, numberTest=123)