Skip to content

Commit

Permalink
Add documentation about compatibility with pydantic 1.x BaseSettings …
Browse files Browse the repository at this point in the history
…when using dotenv (#127)

Co-authored-by: Hasan Ramezani <[email protected]>
  • Loading branch information
SthPhoenix and hramezani authored Jul 13, 2023
1 parent 2572a18 commit 5acbdeb
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,20 @@ Because python-dotenv is used to parse the file, bash-like semantics such as `ex
(depending on your OS and environment) may allow your dotenv file to also be used with `source`,
see [python-dotenv's documentation](https://saurabh-kumar.com/python-dotenv/#usages) for more details.

Pydantic settings consider `extra` config in case of dotenv file. It means if you set the `extra=forbid`
Pydantic settings consider `extra` config in case of dotenv file. It means if you set the `extra=forbid` (*default*)
on `model_config` and your dotenv file contains an entry for a field that is not defined in settings model,
it will raise `ValidationError` in settings construction.

For compatibility with pydantic 1.x BaseSettings you should use `extra=ignore`:
```py
from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
model_config = SettingsConfigDict(env_file='.env', extra='ignore')
```


## Secrets

Placing secret values in files is a common pattern to provide sensitive configuration to an application.
Expand Down

0 comments on commit 5acbdeb

Please sign in to comment.