Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skip empty choice value #687

Merged
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
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
Loading