简体   繁体   中英

How to create a base model in fastapi

I'm working on a pydantic BaseModel.
I wonder how to write the BaseModel to get API called like the example example.

class ResponseModel(BaseModel):
    code : int = Field(title="200")
    message : str = Field(title="success")
    data : object
    custom_lasik : Optional[float] = Field(title="test")
    custom_lasek : Optional[float] = Field(title="test")
    lasik : Optional[float] = Field(title="test")
    lasek : Optional[float] = Field(title="test")
    smile_lasik : Optional[float] = Field(title="test")
    toric : Optional[float] = Field(title="test")
    normal : Optional[float] = Field(title="test")
    class Config:
        schema_extra = {
            "example" : {
                "code": 200,
                "message": 'success',
                "data": {
                    "custom_lasik" : 0.0,
                    "custom_lasek" : 0.0,
                    "lasik" : 0.5,
                    "lasek" : 0.35,
                    "smile_lasik" : 0.15,
                    "toric" : 0.0,
                    "normal" : 0.0
                }    
            }
    }

在此处输入图像描述

I think the value of db comes out because I connected db.
Also, I think there is a null because I set the Optional, but I am thinking that the result is coming out twice because something went wrong somewhere.

class Response(BaseModel):
    custom_lasik : Optional[float] = Field(title="test")
    custom_lasek : Optional[float] = Field(title="test")
    lasik : Optional[float] = Field(title="test")
    lasek : Optional[float] = Field(title="test")
    smile_lasik : Optional[float] = Field(title="test")
    toric : Optional[float] = Field(title="test")
    normal : Optional[float] = Field(title="test")
    

class ResponseModel(BaseModel):
    code : int = Field(title="200")
    message : str = Field(title="success")
    data : Optional[Response] = None
    class Config:
        schema_extra = {
            "example" : {
                "code": 200,
                "message": 'success',
                "data": {
                    "custom_lasik" : 0.0,
                    "custom_lasek" : 0.0,
                    "lasik" : 0.5,
                    "lasek" : 0.35,
                    "smile_lasik" : 0.15,
                    "toric" : 0.0,
                    "normal" : 0.0
                }
            }
        }


I found the answer and close the question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM