我正在尝试创建一个自定义SQLAlchemy选择类型,如下所示:
status = Column(MyCustomType(["accepted", "rejected", "pending"]))
我尝试了以下课程,但没有奏效:
class Selection(types.TypeDecorator):
"""
Selection Field Type
"""
impl = types.String
vals = []
def __init__(self, vals=None):
super(types.TypeDecorator, self).__init__()
self.vals = vals
def process_bind_param(self, value, dialect):
if not value in self.vals : raise ValueError('{} is not in selection field'.format(value))
return value
def process_result_value(self, value, dialect):
return value