Avec un modèle Pydantic comme celui-ci :
pydantic_iter_fields.py
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")
# Ces deux champs seront ignorés dans dict()
_default_paths = ['/data/settings.yaml', './settings.yaml']
__config__ = {
"FOO": "BAR"
}
my_model = MyModel()vous pouvez itérer sur tous les champs comme ceci :
iterate_fields.py
dict(fields)Ceci inclura seulement les instances Field(), pas _default_paths ou __config__.
Exemple de sortie :
output.py
{'variable1': 1.5, 'variable2': 'foobar'}Si cet article vous a aidé, pensez à m'offrir un café ou à faire un don via PayPal pour soutenir la recherche et la publication de nouveaux articles sur TechOverflow