Skip to content
Closed
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ Session.vim
*~
# auto-generated tag files
tags

# Pipenv
Pipfile
Pipfile.lock
5 changes: 5 additions & 0 deletions graphene_django/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ def convert_django_field_with_choices(field, registry=None):
meta = field.model._meta
name = to_camel_case("{}_{}".format(meta.object_name, field.name))
choices = list(get_choices(choices))
default = field.get_default()
if default:
choices_include_default = bool([choice for choice in field.choices if choice[0] == default])
if not choices_include_default:
choices.append(('DEFAULT', default, 'default value'))
named_choices = [(c[0], c[1]) for c in choices]
named_choices_descriptions = {c[0]: c[2] for c in choices}

Expand Down
34 changes: 33 additions & 1 deletion graphene_django/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,38 @@ class Meta:
convert_django_field_with_choices(field)


def test_field_with_choices_and_default():
field = models.CharField(
help_text="Language", choices=(("es", "Spanish"), ("en", "English")), default="nl"
)

class TranslatedModel(models.Model):
language = field

class Meta:
app_label = "test"

graphene_type = convert_django_field_with_choices(field)
assert graphene_type._meta.enum.__members__["DEFAULT"].value == "nl"
assert graphene_type._meta.enum.__members__["DEFAULT"].description == "default value"


def test_field_with_default_in_choices():
field = models.CharField(
help_text="Language", choices=(("es", "Spanish"), ("en", "English")), default="es"
)

class TranslatedModel(models.Model):
language = field

class Meta:
app_label = "test"

graphene_type = convert_django_field_with_choices(field)

assert "DEFAULT" not in graphene_type._meta.enum.__members__


def test_should_float_convert_float():
assert_conversion(models.FloatField, graphene.Float)

Expand Down Expand Up @@ -241,7 +273,7 @@ class A(DjangoObjectType):
class Meta:
model = Article

graphene_field = convert_django_field(Reporter.articles.rel,
graphene_field = convert_django_field(Reporter.articles.rel,
A._meta.registry)
assert isinstance(graphene_field, graphene.Dynamic)
dynamic_field = graphene_field.get_type()
Expand Down