mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-07-13 17:52:19 +03:00
add field default value to generated enum
This commit is contained in:
parent
98de592303
commit
4732b34bd7
|
@ -62,8 +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))
|
||||
if field.blank:
|
||||
choices.append(('EMPTY', '', ''))
|
||||
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}
|
||||
|
||||
|
|
|
@ -196,9 +196,9 @@ def test_field_with_choices_collision():
|
|||
convert_django_field_with_choices(field)
|
||||
|
||||
|
||||
def test_field_with_blank():
|
||||
def test_field_with_choices_and_default():
|
||||
field = models.CharField(
|
||||
help_text="Language", choices=(("es", "Spanish"), ("en", "English")), blank=True
|
||||
help_text="Language", choices=(("es", "Spanish"), ("en", "English")), default="nl"
|
||||
)
|
||||
|
||||
class TranslatedModel(models.Model):
|
||||
|
@ -208,8 +208,24 @@ def test_field_with_blank():
|
|||
app_label = "test"
|
||||
|
||||
graphene_type = convert_django_field_with_choices(field)
|
||||
assert graphene_type._meta.enum.__members__["EMPTY"].value == ""
|
||||
assert graphene_type._meta.enum.__members__["EMPTY"].description == ""
|
||||
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():
|
||||
|
|
|
@ -138,7 +138,6 @@ type ArticleEdge {
|
|||
enum ArticleImportance {
|
||||
A_1
|
||||
A_2
|
||||
EMPTY
|
||||
}
|
||||
|
||||
enum ArticleLang {
|
||||
|
@ -180,7 +179,6 @@ enum ReporterAChoice {
|
|||
enum ReporterReporterType {
|
||||
A_1
|
||||
A_2
|
||||
EMPTY
|
||||
}
|
||||
|
||||
type RootQuery {
|
||||
|
|
Loading…
Reference in New Issue
Block a user