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

Commit

Permalink
Cast lazy proxy objects to string when generating Enum types. (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordaneremieff authored Aug 20, 2021
1 parent a28a415 commit 4f83868
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
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

0 comments on commit 4f83868

Please sign in to comment.