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.

Support Enum for Select-field

See original GitHub issue

I have got some use cases where I use a Enum in my models.

Example:

# definitions.py
from enum import Enum

class Gender(Enum):
    male = 'm'
    female = 'f'
# models.py
from definitions import Gender

class Person:
    def __init__(self, gender: Gender):
        self.gender = gender

Now it would be great if I could use the Enum in marshmallow.fields.Select:

# schemas.py
from marshmallow import fields, Schema
from definitions import Gender
from models import Person

class PersonSchema(Schema):
    gender = fields.Select(Gender)

    @staticmethod
    def make_object(data) -> Person:
        return Person(**data)

For backwards-compatibility enum34 could be used.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:27 (18 by maintainers)

github_iconTop GitHub Comments

4reactions
deckar01commented, Feb 4, 2019

The Enum class is part of the standard library in python 3. If the intention is to map a string to an Enum instance, it would make sense for this to be a field rather than just a validator. We could probably support the Enum interface without needing to import enum to preserve compatibility with python 2. A new field doesn’t have to be part of a major release. It could be added in any minor version.

https://docs.python.org/3/library/enum.html

3reactions
justanrcommented, Feb 5, 2019

@deckar01 Supporting the read from and to interface of Enum can be supported without importing the module.

If this is of interest to have in marshmallow itself, I’m happy to contribute the code over. We can reconvene this when y’all are comfortable taking it on and figure out a migration path for it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SelectField Enum support · Issue #338 · wtforms ... - GitHub
This is something we'll consider supporting and adding to the documentation, however currently SelectField does not directly support Enum .
Read more >
Python Flask WTForm SelectField with Enum values 'Not a ...
I'm attempting to submit a form (POST) where a SelectField is populated by all values of a Enum. When I hit 'Submit' i'm...
Read more >
How to Make SelectFields for Prisma enums in RedwoodJS
Cannot get values from the enum to make a select control because Redwood generates the enum as a type -- and that means...
Read more >
PI52344: ENUM VALUE IN A MULTI-SELECT FIELD WITH ...
Prerequisites: OP 7.1.0.1 NOTE: For this working example we will use SOXBusEntity Object Type, but this behavior occurs with all object types. We...
Read more >
Display Select field with data from ENUM table field - Laracasts
Hi, I want to display data from a ENUM DB table column in a Nova select field. Its name is "status" and has...
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