fastapi 및 pydantic을 사용하여 POST API를 빌드 할 때 TypeError : Object of type is not JSON serializable

푸진

FastAPi와 Pydantic을 사용하여 POST API에 대한 요청과 응답을 모델링합니다.

세 가지 클래스를 정의했습니다.

from pydantic import BaseModel, Field
from typing import List, Optional, Dict

class RolesSchema(BaseModel):
    roles_id: List[str]

class HRSchema(BaseModel):
    pk: int
    user_id: str
    worker_id: str
    worker_name: str
    worker_email: str
    schedulable: bool
    roles: RolesSchema
    state: dict

class CreateHR(BaseModel):
    user_id: str
    worker_id: str
    worker_name: str
    worker_email: str
    schedulable: bool
    roles: RolesSchema

그리고 내 API의 프로그램 :

@router.post("/humanResource", response_model=HRSchema)
async def create_humanResource(create: CreateHR):
query = HumanResourceModel.insert().values(
    user_id=create.user_id, 
    worker_id=create.worker_id, 
    worker_name=create.worker_name,
    worker_email=create.worker_email,
    schedulable=create.schedulable,
    roles=create.roles
)
last_record_id = await database.execute(query)
return {"status": "Successfully Created!"}

입력 데이터 형식은 json입니다.

{
     "user_id": "123",
     "worker_id": "010",
     "worker_name": "Amos",
     "worker_email": "[email protected]",
     "schedulable": true,
     "roles": {"roles_id": ["001"]}
}

실행했을 때 TypeError : Object of type RolesSchema is not JSON serializable.

프로그램을 정상 작동으로 수정하려면 어떻게해야합니까?

alex_noname

대신 roles=create.roles.dict()만들기 위해 사용하십시오queryroles=create.roles

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관