FastAPI BaseModel 구성 - 18

2022. 12. 22. 10:39·Develop/FastAPI
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
저작자표시 비영리 변경금지 (새창열림)

'Develop > FastAPI' 카테고리의 다른 글

FastApi Get By UUID - 20  (0) 2022.12.22
FastAPI Get Request - 19  (0) 2022.12.22
FastAPI API 없이 값 추가하기 - 17  (0) 2022.12.22
FastAPI Data Validation(추가) - 16  (0) 2022.12.22
FastAPI Fields / Data Validation - 15  (0) 2022.12.22
'Develop/FastAPI' 카테고리의 다른 글
  • FastApi Get By UUID - 20
  • FastAPI Get Request - 19
  • FastAPI API 없이 값 추가하기 - 17
  • FastAPI Data Validation(추가) - 16
동석해요
동석해요
공부하고 싶은게 많은, 사소한 IT 지식들 공유
    250x250
  • 동석해요
    개발로 자기계발
    동석해요
  • 전체
    오늘
    어제
    • 분류 전체보기 (226)
      • Develop (126)
        • 기초지식 (12)
        • FastAPI (102)
        • Django (11)
      • Database & Data (62)
        • 기초지식 (16)
        • MySQL (29)
        • PostgreSQL (8)
        • 데이터 분석 (9)
      • 인공지능 (11)
        • PyTorch (9)
      • Cloud (8)
        • AWS (4)
        • GCP (2)
      • 버그처리 (14)
      • 회고 & 일상 (5)
  • 인기 글

  • 최근 글

  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.3
동석해요
FastAPI BaseModel 구성 - 18
상단으로

티스토리툴바