Access to generated enum for Django Choices
See original GitHub issueIn my project I have models with Choice fields and I want to include generated enums in Input for mutations.
Currently I have to use something like
@classmethod
def field_with_choices_enum(cls):
return cls._meta.fields["field_with_choices"]._type._of_type
This approach looks like a hack (and it is). It there any other way to access generated enums?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:7
- Comments:6
Top Results From Across the Web
How to use enums as a choice field in django model
Enum member values are a tuple of arguments to use when constructing the concrete data type. Django supports adding an extra string value...
Read more >Using Enum as Model Field Choice in Django - HackerNoon
In this post, I will walk you through using Enum as choices of field with a ... we use list comprehension to generate...
Read more >Python Django Get Enum Choices
In Django, to define choices for a Field in the Model we use Enumeration. There are two ways to store choices using enum...
Read more >27910 (Allow using an Enum class in model Field choices)
I want to limit the input to certain choices so I create a list/tuple of tuples/lists. Since Python 3.4 there is a class...
Read more >Using Enums as Django Model Choices | by Ben Cleary
Enums in Python. We can use python's built in Enum class to create clear, reusable constants not just for models but available to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@dopeboy having trouble finding the official way to do this in those PRs. Do you know how to do it? Here’s the hacky workaround I’ve been using:
Any update on this one? I was hit by this one recently too, could not create my own
graphene.Enum
type, as it was autogenerated, without a clear way to access it in the code. The solution was like above, or create a sibling type, with a different name. I see two options, either disable this autogeneration of choice field like in #541 and let the users create their own enums according to their needs.The second solution would be to provide an easy way to access those autogenerated types with a possibility to disable creation for the selected field. Something like:
However, as shown in #570, accessing the hidden enum may be tricky (either
.type
or.type._of_type
- for me it was the second one, maybe because I useinterfaces=[Node]
? Maybe there are more ways?)