【发布时间】:2022-08-12 12:52:46
【问题描述】:
我对 FastAPI 很陌生。我有一个看起来像这样的请求:
@router.post(\"/\", response_model=EducationInResp)
async def create_Education_account(
education_in: EducationCreation,
current_user=Depends(get_current_user),
has_perm=Depends(user_has_create_perms),
):
现在EducationCreation 数据模型有一个名为customer_id 的字段。我想检查customer id 是否存在于数据库中。现在,我知道我可以在函数本身中手动执行此操作,并且不建议在 Schema 中进行与数据库相关的验证。有没有办法使用dependencies检查数据库中是否存在customer id?有没有这样的东西:
async def check_customer_exist(some_val):
# some operation here to check and raise exception
@router.post(\"/\", response_model=EducationInResp)
async def create_Education_account(
education_in: EducationCreation = Depends(check_customer_exist),
current_user=Depends(get_current_user),
has_perm=Depends(user_has_create_perms),
):
-
是的,你可以做到。您将需要能够访问
check_customer_exists函数中的客户字段并引发HTTPException或返回EducationCreation类型的数据 -
如何访问
check_customer_exists中的customer id?如果check_customer_exists有任何参数,它会引发 422 并表示缺少该值。 @isabi -
Chris\'s 的回复速度比我快,并提供了正确的答案
标签: python flask fastapi pydantic