Skip to content
This repository was archived by the owner on Jul 18, 2024. It is now read-only.

Cast lazy proxy objects to string when generating Enum types. #36

Merged
merged 1 commit into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion djantic/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from enum import Enum
from uuid import UUID

from django.utils.functional import Promise

from pydantic import IPvAnyAddress, Json
from pydantic.fields import FieldInfo, Required, Undefined

Expand Down Expand Up @@ -89,12 +91,17 @@ def ModelSchemaField(field: Any) -> tuple:

else:
if field.choices:
enum_choices = {v: k for k, v in field.choices}
enum_choices = {}
for k, v in field.choices:
if Promise in type(v).__mro__:
v = str(v)
enum_choices[v] = k
python_type = Enum( # type: ignore
f"{field.name.title().replace('_', '')}Enum",
enum_choices,
module=__name__,
)

if field.has_default() and isinstance(field.default, Enum):
default = field.default.value
else:
Expand Down
4 changes: 2 additions & 2 deletions tests/testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ class Attachment(models.Model):


class FoodChoices(models.TextChoices):
BANANA = "ba", "A delicious yellow Banana"
APPLE = "ap", "A delicious red Apple"
BANANA = "ba", _("A delicious yellow Banana")
APPLE = "ap", _("A delicious red Apple")


class GroupChoices(models.IntegerChoices):
Expand Down