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.

Access to generated enum for Django Choices

See original GitHub issue

In 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:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:6

github_iconTop GitHub Comments

2reactions
dspacejscommented, Oct 2, 2019

@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:

PrimaryGoalCategory = PrimaryGoalNode._meta.fields['category']._type._of_type
1reaction
akjanikcommented, Jan 14, 2019

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:

class Meta:
    model = Book
    (exclude_)generated_fields = [<list of fields>]

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 use interfaces=[Node]? Maybe there are more ways?)

Read more comments on GitHub >

github_iconTop 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 >

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