Skip to content

Instantly share code, notes, and snippets.

@fanzeyi
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fanzeyi/18fc24a495c72a68e4b0 to your computer and use it in GitHub Desktop.
Save fanzeyi/18fc24a495c72a68e4b0 to your computer and use it in GitHub Desktop.
Integrate SQLAlchemy with Enum34
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