Relative Content

Tag Archive for pythonpydantic

How can I lazy compute at runtime some properties everytime the model/a field is updated?

I’m learning pydantic and I wish to understand the interaction between Python classes and their properties with the models of pyndatic. In particular I wish to define some instance variables that are calculated lazily at runtime after the model is created and/or everytime the model or a particular field is updated. Here a small play snipper where I tried some things but it fails.
I understood from debugging that first the model is created from the dict, then the validator is run and it needs the field age to be defined already and it is not, so it crashes. Should I define a property for the age field? Also I’m interested in the historical interactions between properties and computed_field w.r.t. when designing entities from stratch. Should I mix them or use only one solution?

How to clear pydantic object to default values?

I need default values to be cleared in a Pydantic model. I cannot recreate the object because, in my use case, it has references that need to be preserved. Could you please show me the best practices for this?

Pydantic can’t find env_file in BaseSettings

project_root │ .env │ └───backend │ └───migrations │ │ │ └───src │ │ config.py │ │ main.py │ │ … │ └───frontend │ … I have an .env file that is in the root folder, and a config.py file that is in project/backend/src I create a Settings class that inherits from pydantic BaseSettings. Inside I […]