Pydantic introduced in version 1.9
-ish env_nested_delimiter
, a mechanism for parsing env vars in a convenient way.
I have this module in settings:
from pydantic import BaseModel, BaseSettings
class SubModel(BaseModel):
v1: str
v2: bytes
v3: int
class Settings(BaseSettings):
v0: str
sub_models: [SubModel]
class Config:
env_nested_delimiter = '__'
print(Settings().dict())
Is there a way to define elements of my sub_models
(a list
of SubModel
instances) in the .env file with delimiters? I've already tried:
SUB_MODELS__0__SUB_MODEL__V2=00
SUB_MODELS__0__SUB_MODEL__V3=01
SUB_MODELS__0__SUB_MODEL__DEEP__V4=01
SUB_MODELS__1__SUB_MODEL__V2=nested-10
SUB_MODELS__1__SUB_MODEL__V3=11
SUB_MODELS__1__SUB_MODEL__DEEP__V4=12
but it did not work. I'm not sure if it's possibile with the current version of pydantic. Maybe delimiters are just restricted to dict-like data structures.
Looking into the source code, it seems not possibile. At least in version 1.9.0.