From a1b7a3c49c53a0cdb98d9dd09ea9a819d6c4da00 Mon Sep 17 00:00:00 2001 From: Phuong Nguyen <7949163+phuongfi91@users.noreply.github.com> Date: Fri, 13 Dec 2024 19:29:01 +0200 Subject: [PATCH] feat: Support optional fields (#5) --- django2pydantic/getter.py | 4 ++++ django2pydantic/mixin.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/django2pydantic/getter.py b/django2pydantic/getter.py index d966a06..d177ca2 100644 --- a/django2pydantic/getter.py +++ b/django2pydantic/getter.py @@ -58,6 +58,10 @@ def _get_prefetched_values(self, key: str) -> Result: if k.startswith(f"{key}__"): values[k[len(key) + 2 :]] = v + # If values is empty, bubble up AttributeError as there's no value for the key + if not values: + raise AttributeError(key) + return values def __init__(self, obj: Any, schema_cls: Any, context: Any = None) -> None: diff --git a/django2pydantic/mixin.py b/django2pydantic/mixin.py index 1a480fe..825806d 100644 --- a/django2pydantic/mixin.py +++ b/django2pydantic/mixin.py @@ -18,7 +18,7 @@ class BaseMixins(BaseModel): "from_attributes": True, "arbitrary_types_allowed": True, "use_enum_values": True, - "validate_default": True, + "validate_default": False, } @model_validator(mode="wrap")