-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for nested settings (#3)
- Add support for nested settings by dumping the contents of `BaseSettings` and `BaseModel` - Update README with more examples and usage information
- Loading branch information
Showing
3 changed files
with
86 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ poetry add django-base-settings | |
In your Django settings file, define a subclass of `DjangoBaseSettings`: | ||
|
||
```python | ||
from django_base_settings import DjangoBaseSettings | ||
|
||
class MySiteSettings(DjangoBaseSettings): | ||
allowed_hosts: list[str] = ["www.example.com"] | ||
debug: bool = False | ||
|
@@ -39,7 +41,74 @@ DEBUG = False | |
DEFAULT_FROM_EMAIL = "[email protected]" | ||
``` | ||
|
||
Field names in `DjangoBaseSettings` are case-insensitive and can be overridden through environment variables. | ||
### Nested Settings | ||
|
||
For more complex configurations, you can define nested settings using Pydantic models: | ||
|
||
```python | ||
from pydantic import Field | ||
from pydantic_settings import BaseSettings | ||
|
||
from django_base_settings import DjangoBaseSettings | ||
|
||
class CacheSettings(BaseSettings): | ||
backend: str = Field("django.core.cache.backends.redis.RedisCache", alias="BACKEND") | ||
location: str = Field("redis://127.0.0.1:6379/1", alias="LOCATION") | ||
|
||
class MySiteSettings(DjangoBaseSettings): | ||
caches: dict[str, CacheSettings] = { | ||
"default": CacheSettings() | ||
} | ||
|
||
my_site_settings = MySiteSettings() | ||
``` | ||
|
||
This configuration is equivalent to: | ||
|
||
```python | ||
CACHES = { | ||
"default": { | ||
"BACKEND": "django.core.cache.backends.redis.RedisCache", | ||
"LOCATION": "redis://127.0.0.1:6379/1", | ||
} | ||
} | ||
``` | ||
|
||
### Environment Variables | ||
|
||
Fields contained within DjangoBaseSettings and BaseSettings objects can be assigned values or have their default overwritten through environment variables, providing flexibility for different deployment environments. | ||
|
||
In this example: | ||
|
||
```python | ||
from django_base_settings import DjangoBaseSettings | ||
|
||
class MySiteSettings(DjangoBaseSettings): | ||
default_from_email: str = "[email protected]" | ||
|
||
my_site_settings = MySiteSettings() | ||
``` | ||
|
||
You can configure the value of default_from_email by creating an environment variable, which will overwrite the default value: | ||
|
||
```bash | ||
export DEFAULT_FROM_EMAIL="[email protected]" | ||
``` | ||
|
||
You can also specify a different environment variable name: | ||
|
||
```python | ||
from pydantic import Field | ||
|
||
from django_base_settings import DjangoBaseSettings | ||
|
||
class MySiteSettings(DjangoBaseSettings): | ||
default_from_email: str = Field("[email protected]", env="DEFAULT_EMAIL") | ||
|
||
my_site_settings = MySiteSettings() | ||
``` | ||
|
||
In this example, setting `DEFAULT_EMAIL` as an environment variable will override the default value of `default_from_email`. | ||
|
||
## License | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "django-base-settings" | ||
version = "1.0.0" | ||
version = "0.2.0" | ||
description = "Use Pydantic to enhance your Django application settings." | ||
authors = ["vsakkas <[email protected]>"] | ||
license = "MIT" | ||
|