question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DjangoFormMutation does not generate enum from forms.ChoiceField

See original GitHub issue
  • What is the current behavior?

GEEKS_CHOICES =(("1", "One"), ("2", "Two"))

class UpdateUserStateInTradeForm(forms.Form):
    state = forms.ChoiceField(choices=GEEKS_CHOICES)

# mutation
class UpdateUserStateInTradeMutation(DjangoFormMutation):
  class Meta:
    form_class = UpdateUserStateInTradeForm


# generated schema

state: String!
  • What is the expected behavior?

state is enumeration

  • Please tell us about your environment:

    • Version: 2.13.0, Django 3.0.1
    • Platform: Linux

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
marcosalcazarcommented, Jan 22, 2021

About this issue, can you guys let us know what’s the proper way to have Enums for a ChoiceField (or CharField with choices) for a DjangoFormMutation? It seems the bug is still there.

1reaction
iorlandinicommented, Jan 25, 2021

@artofhuman that’s a good point. In that case, check the following branch comparison:

https://github.com/graphql-python/graphene-django/compare/main...iorlandini:feature/form-choices-to-enum-conversion

I think it does the job! Just do not forget to use a Django ModelForm instead of a Form because it needs a reference to the model:

def test_form_field_with_choices_convert_enum():
    class TestModel(models.Model):
        field = models.CharField(
            choices=(("choice-0", "Choice 0"), ("choice-1", "Choice 1"))
        )

    class TestType(DjangoObjectType):
        class Meta:
            model = TestModel

    class TestForm(forms.ModelForm):
        class Meta:
            model = TestModel
            fields = ["field"]

    class TestMutation(DjangoFormMutation):
        class Meta:
            form_class = TestForm

    schema = graphene.Schema(mutation=TestMutation)

    _type = schema.get_type("TestModelField")
    assert _type.name == "TestModelField"

    assert _type.values[0].name == "CHOICE_0"
    assert _type.values[0].value == "choice-0"

    assert _type.values[1].name == "CHOICE_1"
    assert _type.values[1].value == "choice-1"
Read more comments on GitHub >

github_iconTop Results From Across the Web

DjangoFormMutation does not generate enum from forms.ChoiceField
DjangoFormMutation does not generate enum from forms.ChoiceField. ... What is the current behavior? ... ChoiceField(choices=GEEKS_CHOICES) # mutation class ...
Read more >
How to use enums as a choice field in django model
If a tuple is not provided, or the last item is not a (lazy) string, the label is automatically generated from the member...
Read more >
Graphene Documentation - Read the Docs
Graphene-Django comes with mutation classes that will convert the fields on Django forms into inputs on a mutation. DjangoFormMutation from ...
Read more >
graphene-django Changelog - PyUp.io
Do not access the internals of `SimpleLazyObject` by pcraciunoiu in ... Make v3 django choice field enum naming default (in v3) by DoctorJohn...
Read more >
How to use the graphene.Enum function in graphene - Snyk
Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found