Closed jacobsvante closed 2 years ago
Yes this would require the choices to be passed to the underlying type (if its Enum). I think this would be a nice feature :+1:
:+1: would be great to have that
I'm currently using: http://techspot.zzzeek.org/2011/01/14/the-enum-recipe/
But I think being able to map python enum
type to sqlalchemy.types.Enum
would be best.
To be clear the following works:
from sqlalchemy import Column, Enum
from .db import Base
class MyModel(Base):
STATUS_CHOICES = (
('published', 'Published'),
('hidden', 'Hidden'),
)
STATUSES = tuple(c[0] for c in STATUS_CHOICES)
status = Column(
ChoiceType(impl=Enum(*STATUSES, name='mymodel_status'), choices=STATUS_CHOICES),
default='published',
nullable=False
)
I'm not sure there could be any more clean way to do this. Can you?
Perhaps passing the kwargs of ChoiceType
to the Enum. Like this:
ChoiceType(impl_class=Enum, name='mymodel_status', choices=STATUS_CHOICES)
# Would be turned into this:
impl = impl_class(*(c[0] for c in choices), **{'name': name})
ChoiceType(impl=impl, choices=choices)
I would very much like this to happen. Currently if I have an enum.Enum
that I want a model to use, I have these choices:
I plan on going with option 3 for now so I can both have my cake and eat it, but it would be nice to make this the default behavior.
Looks like this is now supported?
Agreed, this appears to be implemented. If not, please add a comment to clarify what has not been implemented. Thanks!
I really like the ChoiceType field, it would be nice though to have the values enforced on the db level as well with
sqlalchemy.types.Enum
as the underlying type. What do you think?