graphene icon indicating copy to clipboard operation
graphene copied to clipboard

Django Model charField with choices : query field

Open olref opened this issue 10 years ago • 5 comments

I have some problems when I try to query a Django charField with choices.

I have the following models.py :

class Vehicle(models.Model):
    TYPE_CHOICES = (
        ('MOTO', _('MOTORBIKE')),
        ('CAR', _('car')),
        ('BIKE', _('bike')),
    )

    name = models.CharField(max_length=100)

    vtype = models.CharField(
        _('Vehicle type'),
        max_length=5,
        choices=TYPE_CHOICES,
        default='MOTO',
        help_text=_('Lorem ipsum')
    )

    description = models.TextField(blank=True, default='')

    def __str__(self):
        return self.name

and the following schema.py :

  class VehicleNode(DjangoNode):
      class Meta:
          model = Vehicle

When I try to make this query :

query {
  allVehicles {
    edges {
      node {
        id,
        name,
        vtype
      }
    }
  }
}

I have the following error :

{
  "errors": [
    {
      "message": "Cannot query field \"vtype\" on type \"VehicleNode\".",
      "locations": [
        {
          "line": 7,
          "column": 9
        }
      ]
    }
  ]
}

To be able to query vtype I need to add vtype = String() into my schema like that :

class VehicleNode(DjangoNode):
    vtype  = String()
    class Meta:
        model = Vehicle

I don't know if I missed something ?

olref avatar Jun 14 '16 15:06 olref

Does it work if you define everything as plain strings instead of using _('xxxxx')?

PeteAndersen avatar Jun 23 '16 03:06 PeteAndersen

No, I have the same issue

olref avatar Jul 01 '16 09:07 olref

I have the same issue here

tom-zeit avatar Aug 16 '16 11:08 tom-zeit

Is this issue related to https://github.com/graphql-python/graphene/issues/290?

glusa8 avatar Sep 15 '16 23:09 glusa8

This should be fixed in graphene-django:master. Closing here :)

syrusakbary avatar Nov 15 '16 04:11 syrusakbary