Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to create a field to expose Model Fields' choices #1180

Unanswered
ZuluPro asked this question in Q&A
Discussion options

I have a model field with choices:

CHOICES = [
 ('foo', 'Foo'),
 ('bar', 'Bar'),
]
class MyModel(models.Model):
 ham = models.CharField(max_length=2, choices=CHOICES)

So, in the schema I have the model and its choices. But can I expose the choices ?
I tried this but without success:

MyModelHamType = registry.get_converted_field(models.MyModel._meta.get_field('ham')).get_type()
class Query(object):
 choices = graphene.Field(MyModelHamType)
 def resolve_choices(self, info, **kwargs):
 # What to do here
 return MyModelHamType

The code above gives the following thing:
Request :

query {
 hams
}

Response:

{
 "errors": [
 {
 "message": "Expected a value of type \"MyModelHam\" but received: MyModelHam",
 "path": [
 "hams"
 ]
 }
 ],
 "data": {
 "hams": null
 }
}

It seems that as the object aren't the of the same type (Pythonicly saying), Graphene doesn't want to handle.


I could recreate myself a MyModelHamType with Enum, but it will be duplicate with the existing one autocreated and already in schema.

You must be logged in to vote

Replies: 3 comments

Comment options

@sciyoshi Any news ?

You must be logged in to vote
0 replies
Comment options

Let's take this over to #67. Closing this down.

You must be logged in to vote
0 replies
Comment options

@ZuluPro were you able to fix this issue? Could you explain how?

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
Converted from issue

This discussion was converted from issue #471 on April 14, 2021 20:18.

AltStyle によって変換されたページ (->オリジナル) /