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.

Cannot return null for non-nullable field FooType.barType [Enum]

See original GitHub issue

The error occurs with graphene-django==2.0.0 with a field with choices, for example:

class Foo(models.Model):
    ONE = 1
    TWO = 2
  
    BAR_TYPES = (
        (ONE, _('one'),
        (TWO, _('two'),
    )

    bar_type = models.PositiveIntegerField(_('type'), choices=BAR_TYPES)


class FooType(DjangoObjectType):                                    
    class Meta:
        model = Foo

This error can be resolved with an explicit graphene.Field declaration:

class FooEnum(graphene.Enum):
    ONE = 1
    TWO = 2                       


class FooType(DjangoObjectType):                                    
    bar_type = graphene.Field(FooEnum)
    class Meta:
        model = Foo

Is it really necessary to duplicate the choices, though?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:6

github_iconTop GitHub Comments

2reactions
prokhercommented, Jun 21, 2018

I had the same issue until I applied the following:


import django.db.models as dj_models
from enum import Enum

class MyEnum(Enum):
    VAL1 = 'Value 1'
    VAL2 = 'Value 2'
    VAL3 = 'Value 3'

    def __str__(self):
        """Allow to directly assign enum values to the model field."""
        return self.name


class MyModel(dj_models.Model):
    state = dj_models.CharField(choices=[(tag.name, tag.value) for tag in MyEnum],
                                max_length=42)

this fixed an issues for me.

I’ve got the recipe from here: http://anthonyfox.io/2017/02/choices-for-choices-in-django-charfields/

Also there are useful comments under this article: https://hackernoon.com/using-enum-as-model-field-choice-in-django-92d8b97aaa63

0reactions
stale[bot]commented, Jun 11, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Return ENUM - Cannot return null for non-nullable field
SOLVED: The issue was that I was using Prisma as ORM. When I was getting a new book, I was writing the code:...
Read more >
Cannot return null for non-nullable field FooType.barType [Enum]
Cannot return null for non -nullable field FooType.barType [Enum] ... The error occurs with graphene-django==2.0.0 with a field with choices, for example:
Read more >
Cannot return null for non-nullable type graphql - Fauna Forums
I get the following error when i query allTodos index through graphql "errors": [ { "message": "Cannot return null for non-nullable type ...
Read more >
Extend the Kubernetes API with CustomResourceDefinitions
This page shows how to install a custom resource into the Kubernetes API by creating a CustomResourceDefinition. Before you begin You need ...
Read more >
"Cannot return null for non-nullable field" error - GraphQL API
I have an Item custom class, and I use a custom graphql mutation to update an item via a resolver in Cloud code...
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