0
votes

I'm using pydantic 1.9.0 and fastapi 0.71.0 with Python 3.7.8. Here's how I've defined my model:

class PartModel(BaseModel):
    _id: str 
    _key: str 
    number: str = Field(...)
    name: str = Field(...)
    price: int = Field(...)

    class Config:
        schema_extra = {
            "example": {
                "_key": "12453",
                "number": "5F22",
                "name": "SHA9-169",
                "price": 4,
            }
        }

I would like to be flexible with the Model because my dictionaries are not consisted when it comes to the length. For example one dictionary might have additional key/value pairs. At the moment when i try to make the request through the FastApi it doesn't allow me to POST in the Database. Is there any way to keep my Model flexible/extendable without defining every key in the class?

1
use a wrapper around the db model , and try to work through that, keep fixed/main coloumn there and aditional one in wrappersahasrara62
can you give a an example with code please?msp
sorry, above approach wont work, as once a db schema you have define, and now you want to add data to it having different key -values, then use nosql dbsahasrara62
already using a nosql dbmsp

1 Answers

0
votes

In order to work with flexible models, I would recommend to work with Optional fields, as described in https://pydantic-docs.helpmanual.io/usage/models/#required-optional-fields