Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage Django choices & Enums with DjangoChoicesEnum #630

Closed
wants to merge 3 commits into from

Conversation

phalt
Copy link
Contributor

@phalt phalt commented May 8, 2019

This PR aims to resolve some of the issues seen with choices and enums:

They all want different things, so there isn't going to be a solution that makes everyone happy. This PR proposes a solution that covers most of the complaints.

Major issue 1

Managing the difference between an enum and a choice list.

Django's choices accept a list of tuples. Graphene's enums don't support this. How can we find a happy medium?

Solution: Implement a helper class that manages the conversion for us.

from graphene_django import DjangoChoicesEnum

class MyChoices(DjangoChoicesEnum):
    FOO = 'foo'
    BAR = 'bar'

# Get a Django choices list
MyChoices.choices()

# Get a Graphene enum
MyChoices.enum()

Major issue 2

How do I see my enums?

We've documented a way of accessing enums through the API, in the documentation.

@coveralls
Copy link

coveralls commented May 8, 2019

Coverage Status

Coverage decreased (-0.3%) to 93.654% when pulling beb422f on custom-choices into 223d0b1 on master.

@phalt phalt changed the title Manage Django choices & Enums better. Manage Django choices & Enums with DjangoChoicesEnum May 8, 2019
@phalt phalt added this to In progress in Improvements May 8, 2019
@phalt phalt moved this from In progress to In review in Improvements May 8, 2019
@ProjectCheshire
Copy link
Member

Hah! I had this same issue with neomodel choices

@zbyte64
Copy link
Collaborator

zbyte64 commented May 28, 2019

I wonder if it makes more sense to have DjangoChoicesEnum directly inherit from graphene's Enum , not sure if all the metamagic is in the way.

Edit: Another alternative implementation would be to have a utility function that generates django choices from graphene enums. This would feel less opinionated.

@zbyte64
Copy link
Collaborator

zbyte64 commented May 31, 2019

The user still needs to define the enum field at the DjangoObjectType for this to work, which I think is the proper way forward. We might want to document that use case.

@zbyte64
Copy link
Collaborator

zbyte64 commented Jun 5, 2019

Came across another project's attempt to mediate enums and choices, I like the usage pattern here: https://github.com/jazzband/django-model-utils/blob/master/docs/utilities.rst#choices

@phalt phalt closed this Jun 7, 2019
@phalt phalt deleted the custom-choices branch June 20, 2019 10:27
@jkimbo jkimbo removed this from In review in Improvements Jul 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants