FastAPI - return 来自数据库的单个元素
FastAPI - return Single element in from Database
我有 2 个 api 端点。这个 return 是我所有用户的列表。
@app.get("/entity/", response_model=List[schemas.Userentity])
def read_entities(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
users = crud.get_entities(db, skip=skip, limit=limit)
return users
def get_entities(db: Session, skip: int = 0, limit: int = 100):
return db.query(models.Userentity).offset(skip).limit(limit).all()
这很好用。查看数据:
[
{
"id": "89f8a844-a30e-41b2-947d-6eb0fcadb1bf",
"email": "xxx@hotmail.com",
"first_name": "xxx",
"last_name": "xxx",
"username": "schoedeld",
"created_timestamp": "1633711745164"
},
{
"id": "abd6bb6b-ad80-431c-9f64-d9e651638f4c",
"email": "xxxsd@hotmail.com",
"first_name": "xxx",
"last_name": "xxx",
"username": "xxx",
"created_timestamp": "1633711733338"
},
{
"id": "ad0bd5ca-5aed-4d7f-a6ac-2620f2133685",
"email": "xxxx@hotmail.com",
"first_name": "fsdfs",
"last_name": "fsdfs",
"username": "testadmin",
"created_timestamp": "1633710812666"
}
]
这里我有一个指向 return 单个用户的端点:
我可以 return 一个包含单个元素的列表,但我想 return 一个值,所以我将端点更新为:
@app.get("/entity/{id}", response_model=schemas.Userentity)
def read_entity(id: str, db: Session = Depends(get_db)):
user = crud.get_entity(db, id=id)
return user
def get_entity(db: Session, id: str):
return db.query(models.Userentity).filter_by(id=id)
这会导致以下错误:
pydantic.error_wrappers.ValidationError: 6 validation errors for Userentity
response -> id
field required (type=value_error.missing)
response -> email
field required (type=value_error.missing)
response -> first_name
field required (type=value_error.missing)
response -> last_name
field required (type=value_error.missing)
response -> username
field required (type=value_error.missing)
response -> created_timestamp
field required (type=value_error.missing)
我是 FastAPI 的新手,不明白为什么会出现这个错误,谁能帮我解释一下我做错了什么?
您实际上从未运行查询您的 SQLAlchemy 查询,所以您 return查询本身:
return db.query(models.Userentity).filter_by(id=id)
相反,您应该通过以下任一方式使 SQLAlchemy 运行 成为您的查询:
# raise an exception if not exactly one row is returned (so more than one or zero)
return db.query(models.Userentity).filter_by(id=id).one()
# Either one or zero rows - return `None` if user wasn't found
return db.query(models.Userentity).filter_by(id=id).one_or_none()
# Return None if user isn't found, otherwise return the first row
# (multiple rows isn't an error)
return db.query(models.Userentity).filter_by(id=id).first()
下次在您的视图函数的 return
调用处附加设置调试器断点(或至少 print
值),您可以准确地看到您要 return 到 FastAPI 进行序列化。
我有 2 个 api 端点。这个 return 是我所有用户的列表。
@app.get("/entity/", response_model=List[schemas.Userentity])
def read_entities(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
users = crud.get_entities(db, skip=skip, limit=limit)
return users
def get_entities(db: Session, skip: int = 0, limit: int = 100):
return db.query(models.Userentity).offset(skip).limit(limit).all()
这很好用。查看数据:
[
{
"id": "89f8a844-a30e-41b2-947d-6eb0fcadb1bf",
"email": "xxx@hotmail.com",
"first_name": "xxx",
"last_name": "xxx",
"username": "schoedeld",
"created_timestamp": "1633711745164"
},
{
"id": "abd6bb6b-ad80-431c-9f64-d9e651638f4c",
"email": "xxxsd@hotmail.com",
"first_name": "xxx",
"last_name": "xxx",
"username": "xxx",
"created_timestamp": "1633711733338"
},
{
"id": "ad0bd5ca-5aed-4d7f-a6ac-2620f2133685",
"email": "xxxx@hotmail.com",
"first_name": "fsdfs",
"last_name": "fsdfs",
"username": "testadmin",
"created_timestamp": "1633710812666"
}
]
这里我有一个指向 return 单个用户的端点:
我可以 return 一个包含单个元素的列表,但我想 return 一个值,所以我将端点更新为:
@app.get("/entity/{id}", response_model=schemas.Userentity)
def read_entity(id: str, db: Session = Depends(get_db)):
user = crud.get_entity(db, id=id)
return user
def get_entity(db: Session, id: str):
return db.query(models.Userentity).filter_by(id=id)
这会导致以下错误:
pydantic.error_wrappers.ValidationError: 6 validation errors for Userentity
response -> id
field required (type=value_error.missing)
response -> email
field required (type=value_error.missing)
response -> first_name
field required (type=value_error.missing)
response -> last_name
field required (type=value_error.missing)
response -> username
field required (type=value_error.missing)
response -> created_timestamp
field required (type=value_error.missing)
我是 FastAPI 的新手,不明白为什么会出现这个错误,谁能帮我解释一下我做错了什么?
您实际上从未运行查询您的 SQLAlchemy 查询,所以您 return查询本身:
return db.query(models.Userentity).filter_by(id=id)
相反,您应该通过以下任一方式使 SQLAlchemy 运行 成为您的查询:
# raise an exception if not exactly one row is returned (so more than one or zero)
return db.query(models.Userentity).filter_by(id=id).one()
# Either one or zero rows - return `None` if user wasn't found
return db.query(models.Userentity).filter_by(id=id).one_or_none()
# Return None if user isn't found, otherwise return the first row
# (multiple rows isn't an error)
return db.query(models.Userentity).filter_by(id=id).first()
下次在您的视图函数的 return
调用处附加设置调试器断点(或至少 print
值),您可以准确地看到您要 return 到 FastAPI 进行序列化。