Last active
August 29, 2015 14:09
-
-
Save fanzeyi/18fc24a495c72a68e4b0 to your computer and use it in GitHub Desktop.
Integrate SQLAlchemy with Enum34
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Enum34(types.TypeDecorator): | |
impl = types.Integer | |
def __init__(self, enum_class, *args, **kwargs): | |
super(Enum34, self).__init__(*args, **kwargs) | |
self._enum_class = enum_class | |
def process_bind_param(self, value, dialect): | |
if value not in self._enum_class: | |
raise ValueError("Not a valid enum value") | |
return value.value | |
def process_result_value(self, value, dialect): | |
return self._enum_class(value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment