如何解决乌龟中的 from_queryset_single 与 from_tortoise_orm快速 api
我是 Fast API 的新手,我正在尝试创建一个 API 来返回数据库中的学生。
以下代码似乎有效
@app.get("/{id}")
async def get_student(id:int):
return await stud_pydantic.from_queryset_single(Student.get(id=id))
这似乎也有效
@app.get("/{id}")
async def get_student(id:int):
stud_obj = Student.get(id=id)
return await stud_pydantic.from_tortoise_orm(stud_obj)
但是,这不起作用
@app.get("/{id}")
async def get_student(id:int):
stud_obj = Student.get(id=id)
return await stud_pydantic.from_queryset_single(stud_obj)
但是,两者基本上都试图返回一个学生对象。对 ?那么,有什么区别。 似乎我不明白 from_queryset_single 和 from_tortoise_orm 方法之间的区别
这是我的学生模型
class Student(models.Model):
name = fields.CharField(50,unique=True)
age = fields.IntField()
id = fields.IntField(pk=True)
stud_pydantic = pydantic_model_creator(Student,name="student")
studRO_pydantic = pydantic_model_creator(Student,name="studentRO",exclude_readonly=True)
提前致谢
解决方法
我认为您需要等待从数据库中获取数据的过程。
@app.get("/{id}")
async def get_student(id:int):
stud_obj = await Student.get(id=id)
return await stud_pydantic.from_queryset_single(stud_obj)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。