Skip to content

Commit

Permalink
fix: use str() to trigger eventual django's gettext_lazy string (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabien-michel authored Mar 4, 2024
1 parent 42a3183 commit fc23726
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
7 changes: 3 additions & 4 deletions strawberry_django/fields/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
Type,
TypeVar,
Union,
cast,
)

import django
Expand Down Expand Up @@ -445,7 +444,6 @@ def resolve_model_field_type(
int,
) # Exclude IntegerChoices
):
choices = cast(List[Tuple[Any, str]], model_field.choices)
field_type = getattr(model_field, "_strawberry_enum", None)
if field_type is None:
meta = model_field.model._meta
Expand All @@ -460,8 +458,9 @@ def resolve_model_field_type(
),
),
{
c[0]: EnumValueDefinition(value=c[0], description=c[1])
for c in choices
# use str() to trigger eventual django's gettext_lazy string
c[0]: EnumValueDefinition(value=c[0], description=str(c[1]))
for c in model_field.choices
},
),
description=(
Expand Down
13 changes: 7 additions & 6 deletions tests/test_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import strawberry
from django.db import models
from django.test import override_settings
from django.utils.translation import gettext_lazy
from django_choices_field import IntegerChoicesField, TextChoicesField
from pytest_mock import MockerFixture

Expand All @@ -18,15 +19,15 @@ class Choice(models.TextChoices):

A = "a", "A description"
B = "b", "B description"
C = "c", "C description"
C = "c", gettext_lazy("C description")


class IntegerChoice(models.IntegerChoices):
"""IntegerChoice description."""

X = 1, "1 description"
Y = 2, "2 description"
Z = 3, "3 description"
Z = 3, gettext_lazy("3 description")


class ChoicesModel(models.Model):
Expand All @@ -36,13 +37,13 @@ class ChoicesModel(models.Model):
max_length=255,
choices=[
("c", "C description"),
("d", "D description"),
("d", gettext_lazy("D description")),
],
)
attr4 = models.IntegerField(
choices=[
(4, "4 description"),
(5, "5 description"),
(5, gettext_lazy("5 description")),
],
)
attr5 = models.CharField(
Expand All @@ -61,13 +62,13 @@ class ChoicesWithExtraFieldsModel(models.Model):
max_length=255,
choices=[
("c", "C description"),
("d", "D description"),
("d", gettext_lazy("D description")),
],
)
attr4 = models.IntegerField(
choices=[
(4, "4 description"),
(5, "5 description"),
(5, gettext_lazy("5 description")),
],
)
attr5 = models.CharField(
Expand Down

0 comments on commit fc23726

Please sign in to comment.