我是FastAPI的新手,我正在尝试创建一个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)
但是,两者基本上都试图返回一个student对象。正当那么,有什么区别呢。 似乎我不明白从“queryset”到“single”和“Turtoise”的方法有什么不同
这是我的学生模型
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)
提前谢谢
我认为您需要等待从数据库获取数据的过程
相关问题 更多 >
编程相关推荐