From 390439c96cfc46c1c3a282ee378e7cbad29acd42 Mon Sep 17 00:00:00 2001 From: q0w <43147888+q0w@users.noreply.github.com> Date: Wed, 20 Dec 2023 17:39:50 +0500 Subject: [PATCH] Dump computed fields (#22) --- pydjantic/pydjantic.py | 2 +- tests/test_to_django.py | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pydjantic/pydjantic.py b/pydjantic/pydjantic.py index 21d0294..956fcf5 100644 --- a/pydjantic/pydjantic.py +++ b/pydjantic/pydjantic.py @@ -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) diff --git a/tests/test_to_django.py b/tests/test_to_django.py index b5209a2..b6a73df 100644 --- a/tests/test_to_django.py +++ b/tests/test_to_django.py @@ -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 @@ -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"