'dependency injection data model fastapi
I'm very new to fastapi. I've a request which looks something like this
@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),
):
Now the EducationCreation
data model has a field called customer_id
I want to check if the customer id exists in the database. Now I know that I can manually do that within the function itself and it's not recommended to do database related validation in Schema
. Is there any way to check if the customer id
exists in the database using dependencies
. Is there something like this?
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),
):
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|