Develop/FastAPI

FastAPI BaseModel 구성 - 18

동석해요 2022. 12. 22. 10:39
728x90
SMALL

1. BaseModel의 구성 정의

  • Book 클래스에 값을 넣을 때 구성이 어떻게 되는지, 어떤 값들이 들어가는지 예시를 보여줄 수 있다.
class Book(BaseModel):
    id: UUID
    title: str = Field(min_length=1)
    author: str = Field(min_length=1, max_length=100)
    description: Optional[str] = Field(title="Description of the book",
                             max_length=100,
                             min_length=1)
    rating: int = Field(gt=-1, lt=101)

#Config가 Book 클래스의 Example Values
    class Config:
        schema_extra = {
            "example":{
                "id" : "637d1b93-0174-48e7-8959-e17530b6c690",
                "title" : "Computer Science Pro",
                "author" : "Codingwithroby",
                "description" : "A very nice description of a book",
                "rating" : 75
            }
        }

 

2. Swagger 확인

  • Example Value에 Config 클래스로 정의한 값들을 볼 수 있다.
  • Schema
 
 
728x90
SMALL