Skip to content

Commit

Permalink
Dump computed fields (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
q0w authored Dec 20, 2023
1 parent ecb6bf7 commit 390439c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pydjantic/pydjantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ def _get_actual_value(val: Any):
else:
return val

for key, value in settings:
for key, value in settings.model_dump().items():
parent_frame.f_locals[key] = _get_actual_value(value)
15 changes: 14 additions & 1 deletion tests/test_to_django.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any, Dict, List

from deepdiff import DeepDiff
from pydantic import Field, PostgresDsn, SecretStr, ValidationInfo, field_validator
from pydantic import Field, PostgresDsn, SecretStr, ValidationInfo, computed_field, field_validator
from pydantic_settings import BaseSettings

from pydjantic import BaseDBConfig, to_django
Expand Down Expand Up @@ -128,3 +128,16 @@ class Settings(BaseSettings):
],
)
assert diff == {}


def test_to_django_computed_fields():
class StorageSettings(BaseSettings):
FILE_STORAGE: str

@computed_field
def STORAGES(self) -> dict[str, dict[str, str]]:
return {"default": {"BACKEND": self.FILE_STORAGE}}

settings = StorageSettings(FILE_STORAGE="ExampleStorage")
to_django(settings)
assert locals()["STORAGES"]["default"]["BACKEND"] == "ExampleStorage"

0 comments on commit 390439c

Please sign in to comment.