Professor
Professor

Reputation: 101

pydantic: Reading model with values not in model

Is it possible to define a model using pydantic that allows values not specified in the model?

For example I have a json object:

{
 "object_id": "x",
 "object_type": "some_type",
 "additional-key1": 1,
 "additional-key2": "abc"
}

and I want it to be defined using

class MyObject(BaseModel):
    object_id: str
    object_type: str

But retain all the other additional keys?

Upvotes: 1

Views: 270

Answers (1)

Samuel Colvin
Samuel Colvin

Reputation: 13349

yes

class MyObject(BaseModel):
    object_id: str
    object_type: str

    class Config:
        extra = 'allow'

See the docs on Config.

Upvotes: 3

Related Questions