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.

Bug with Django ChoiceField

See original GitHub issue

Hi, hi I got one bug using models.CharField with choices. Graphene is creating two differents objects as Enum. Raising one error in graphene.types.typemap

File "/usr/local/lib/python2.7/dist-packages/graphene-1.1.3-py2.7.egg/graphene/types/typemap.py", line 64, in reducer
    return self.graphene_reducer(map, type)
  File "/usr/local/lib/python2.7/dist-packages/graphene-1.1.3-py2.7.egg/graphene/types/typemap.py", line 82, in graphene_reducer
    return self.construct_objecttype(map, type)
  File "/usr/local/lib/python2.7/dist-packages/graphene-1.1.3-py2.7.egg/graphene/types/typemap.py", line 153, in construct_objecttype
    map[type._meta.name]._fields = self.construct_fields_for_type(map, type)
  File "/usr/local/lib/python2.7/dist-packages/graphene-1.1.3-py2.7.egg/graphene/types/typemap.py", line 214, in construct_fields_for_type
    map = self.reducer(map, field.type)
  File "/usr/local/lib/python2.7/dist-packages/graphene-1.1.3-py2.7.egg/graphene/types/typemap.py", line 64, in reducer
    return self.graphene_reducer(map, type)
  File "/usr/local/lib/python2.7/dist-packages/graphene-1.1.3-py2.7.egg/graphene/types/typemap.py", line 82, in graphene_reducer
    return self.construct_objecttype(map, type)
  File "/usr/local/lib/python2.7/dist-packages/graphene-1.1.3-py2.7.egg/graphene/types/typemap.py", line 153, in construct_objecttype
    map[type._meta.name]._fields = self.construct_fields_for_type(map, type)
  File "/usr/local/lib/python2.7/dist-packages/graphene-1.1.3-py2.7.egg/graphene/types/typemap.py", line 214, in construct_fields_for_type
    map = self.reducer(map, field.type)
  File "/usr/local/lib/python2.7/dist-packages/graphene-1.1.3-py2.7.egg/graphene/types/typemap.py", line 64, in reducer
    return self.graphene_reducer(map, type)
  File "/usr/local/lib/python2.7/dist-packages/graphene-1.1.3-py2.7.egg/graphene/types/typemap.py", line 69, in graphene_reducer
    return self.reducer(map, type.of_type)
  File "/usr/local/lib/python2.7/dist-packages/graphene-1.1.3-py2.7.egg/graphene/types/typemap.py", line 64, in reducer
    return self.graphene_reducer(map, type)
  File "/usr/local/lib/python2.7/dist-packages/graphene-1.1.3-py2.7.egg/graphene/types/typemap.py", line 79, in graphene_reducer
    assert _type.graphene_type == type
AssertionError
********************************************************************************
_type.graphene_type  UserMobile1Os 139869850885568
type UserMobile1Os 139869850674512
********************************************************************************

My model

MOBILE_PLATFORMS_CHOICES = (
    ('android', 'Android'),
    ('ios', 'iOS'),
)


class User(AbstractUser):
    full_name = models.CharField(max_length=64, blank=True)
    mobile_1_os = models.CharField(max_length=10, default='android',
                                   choices=MOBILE_PLATFORMS_CHOICES,
                                   db_index=True)

My query

class Query(graphene.AbstractType):
    users = DjangoConnectionField(
        UserNode
    )

My UserNode

class UserNode(DjangoObjectType):
    class Meta(object):
        interfaces = (relay.Node,)
        model = User

And I have one file for Schemas

class AllQuery(Query, ObjectType):

   viewer = graphene.Field(lambda: AllQuery)

    @classmethod
    def resolve_viewer(cls, *args, **kwargs):
        return AllQuery()

schema_graphene = graphene.Schema(query=AllQuery)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
jsepcommented, Dec 13, 2017

I have the same issue but I found workaround.

  1. Exclude the choices property
  2. Create an Enum for the choices
  3. In the node, create a property with the same name as the choice property and use graphene.Enum
  4. Create a resolver for the property in the node

See example below. Hope this helps.


class ModelWithChoices(models.Model):
    name = models.CharField(max_length=10)
    choices_property = models.CharField(max_length=20, null=True, blank=True,
                                         choices=(("choice_one", "choice"),
                                                        ("other_choice", "other choice"),) )


class ChoicesPropertyEnum(graphene.Enum):
    CHOICE_ONE = "choice"
    OTHER_ONE = "other choice"


class ModelWithChoicesNode(DjangoObjectType):

    choices_property = ChoicesPropertyEnum()

    class Meta:
        model = ModelWithChoices
        interfaces = [relay.Node]
        exclude_fields = ['choices_property']

    def resolve_choices_property(self, info, **kwargs):
        return self.choices_property
2reactions
mohamedrezcommented, Sep 5, 2018

another way that I tried and it worked:

    choices_property = graphene.String()

    def resolve_choices_property(self, info, **kwargs):
        return self.get_choices_property_display()

No need to excucled the field

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django choice field unexpected behaviour (bug?)
I came across some weird behaviour in the migrations with Django 1.7. Is this a legitimate bug that should be reported?
Read more >
Choices field has a bug - Using Django
I have this model: from django.db import models class TestField(models.Model): M = 'Male' F = 'Female' O = 'Other' GENDER = [ (M,...
Read more >
ModelChoiceField and ChoiceField clean methods behave ...
Using both ChoiceField and ModelChoiceField, I discovered a bug in ChoiceField ... I modified the fields.py file in django/newforms, the clean method on...
Read more >
#5327 (ChoiceField clean method) – Django - Django's bug tracker
Using both ChoiceField and ModelChoiceField, I discovered a bug in ChoiceField clean method ( or a discrepancy in behaviour). ModelChoiceField seems to be ......
Read more >
Form fields - Django documentation
See the model field reference documentation on choices for more details. ... Ticket tracker: Report bugs with Django or Django documentation in our...
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