From 266895a61f6d7eef3c4032da21d39e110b834c65 Mon Sep 17 00:00:00 2001 From: Fabien MICHEL Date: Tue, 7 Jan 2025 18:34:39 +0100 Subject: [PATCH] fix: skip empty choice value (#687) --- strawberry_django/fields/types.py | 6 +++++- tests/test_enums.py | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/strawberry_django/fields/types.py b/strawberry_django/fields/types.py index b1cf2b41..b7d365af 100644 --- a/strawberry_django/fields/types.py +++ b/strawberry_django/fields/types.py @@ -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 diff --git a/tests/test_enums.py b/tests/test_enums.py index 9dc86d0d..51d491cf 100644 --- a/tests/test_enums.py +++ b/tests/test_enums.py @@ -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):