Skip to content

Commit

Permalink
fix: skip empty choice value (#687)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabien-michel authored Jan 7, 2025
1 parent ba27440 commit 266895a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
6 changes: 5 additions & 1 deletion strawberry_django/fields/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,11 @@ def resolve_model_field_type(
meta = model_field.model._meta

enum_choices = {}
for c in cast("Iterable[tuple[str, str]]", model_field.choices):
for c in cast("Iterable[tuple[str | None, str]]", model_field.choices):
# Skip empty choice (__empty__)
if not c[0]:
continue

# replace chars not compatible with GraphQL naming convention
choice_name = re.sub(r"^[^_a-zA-Z]|[^_a-zA-Z0-9]", "_", c[0])
# use str() to trigger eventual django's gettext_lazy string
Expand Down
1 change: 1 addition & 0 deletions tests/test_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Choice(models.TextChoices):
C = "c", gettext_lazy("C description")
D = "12d-d'éléphant_🐘", "D description"
E = "_2d_d__l_phant__", "E description"
__empty__ = "Empty"


class IntegerChoice(models.IntegerChoices):
Expand Down

0 comments on commit 266895a

Please sign in to comment.