选择列SQLAlchemy

时间:2015-02-03 16:44:01

标签: python sqlalchemy

我正在尝试创建一个自定义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

0 个答案:

没有答案