Support Enum for Select-field
See original GitHub issueI 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:
- Created 8 years ago
- Comments:27 (18 by maintainers)
Top 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 >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
The
Enum
class is part of the standard library in python 3. If the intention is to map a string to anEnum
instance, it would make sense for this to be a field rather than just a validator. We could probably support theEnum
interface without needing to importenum
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
@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.