Skip to content

Commit

Permalink
Update nested model partial update docs example. (pydantic#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
kschwab authored Oct 2, 2024
1 parent 8f7653c commit 0d605d0
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,18 +390,36 @@ class SubModel(BaseModel):
flag: bool = False


class Settings(BaseSettings):
class SettingsPartialUpdate(BaseSettings):
model_config = SettingsConfigDict(
env_nested_delimiter='__', nested_model_default_partial_update=True
)

nested_model: SubModel = SubModel()
nested_model: SubModel = SubModel(val=1)


class SettingsNoPartialUpdate(BaseSettings):
model_config = SettingsConfigDict(
env_nested_delimiter='__', nested_model_default_partial_update=False
)

nested_model: SubModel = SubModel(val=1)


# Apply a partial update to the default object using environment variables
os.environ['NESTED_MODEL__FLAG'] = 'True'

assert Settings().model_dump() == {'nested_model': {'val': 0, 'flag': True}}
# When partial update is enabled, the existing SubModel instance is updated
# with nested_model.flag=True change
assert SettingsPartialUpdate().model_dump() == {
'nested_model': {'val': 1, 'flag': True}
}

# When partial update is disabled, a new SubModel instance is instantiated
# with nested_model.flag=True change
assert SettingsNoPartialUpdate().model_dump() == {
'nested_model': {'val': 0, 'flag': True}
}
```

## Dotenv (.env) support
Expand Down

0 comments on commit 0d605d0

Please sign in to comment.