依赖注入数据模型 fastapi
dependency injection data model fastapi
我对 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
的字段
我想检查数据库中是否存在客户 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),
):
您可以通过在依赖函数中声明参数来做到这一点,如 documentation. If the customer_id
exists in the database, then return the data to the route. If not, you could then raise an HTTPException
中所述,或者根据需要进行处理。
from fastapi.exceptions import HTTPException
customer_ids = [1, 2, 3]
async def check_customer_exist(education_in: EducationCreation):
if education_in.customer_id not in customer_ids: # here, check if the customer id exists in the database.
raise HTTPException(status_code=404, detail="Customer ID not found")
else:
return education_in
@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),
):
我对 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
我想检查数据库中是否存在客户 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),
):
您可以通过在依赖函数中声明参数来做到这一点,如 documentation. If the customer_id
exists in the database, then return the data to the route. If not, you could then raise an HTTPException
中所述,或者根据需要进行处理。
from fastapi.exceptions import HTTPException
customer_ids = [1, 2, 3]
async def check_customer_exist(education_in: EducationCreation):
if education_in.customer_id not in customer_ids: # here, check if the customer id exists in the database.
raise HTTPException(status_code=404, detail="Customer ID not found")
else:
return education_in
@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),
):