How to iterate over all fields in Pydantic model
With a Pydantic model like this one:
from pydantic import BaseModel, Field
class MyModel(BaseModel):
variable1: float = Field(default=1.5, help="TODO enter your description here")
variable2: str = Field(default="foobar", help="TODO enter your description here")
# These two fields will be ignored in dict()
_default_paths = ['/data/settings.yaml', './settings.yaml']
__config__ = {
"FOO": "BAR"
}
my_model = MyModel()
you can iterate over all fields like this:
dict(fields)
This will only include the Field()
instances, not _default_paths
or __config__
.
Example output:
{'variable1': 1.5, 'variable2': 'foobar'}