Skip to content

Commit 5acbdeb

Browse files
Add documentation about compatibility with pydantic 1.x BaseSettings when using dotenv (#127)
Co-authored-by: Hasan Ramezani <[email protected]>
1 parent 2572a18 commit 5acbdeb

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

docs/index.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,20 @@ Because python-dotenv is used to parse the file, bash-like semantics such as `ex
383383
(depending on your OS and environment) may allow your dotenv file to also be used with `source`,
384384
see [python-dotenv's documentation](https://saurabh-kumar.com/python-dotenv/#usages) for more details.
385385

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

390+
For compatibility with pydantic 1.x BaseSettings you should use `extra=ignore`:
391+
```py
392+
from pydantic_settings import BaseSettings, SettingsConfigDict
393+
394+
395+
class Settings(BaseSettings):
396+
model_config = SettingsConfigDict(env_file='.env', extra='ignore')
397+
```
398+
399+
390400
## Secrets
391401

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

0 commit comments

Comments
 (0)