更新sqlalchemy orm时的验证不起作用

时间:2014-02-23 15:28:42

标签: python sqlalchemy flask-sqlalchemy

我有这堂课:

class User(base):
    __tablename__='User'
    name = Column(.......

    def __init__(self, name):
        self.name = name

    @validates('name')
    def validate_name(self, key, name):
        if blah blah blah:
            return name
        else:
            raise exception.....

创建一个新用户并将他存储在数据库中......

if __name__ == '__main__':
    user = User('foo')
    session.add(user)
    session.commit() #validation works here

更新用户时:

if __name__ == '__main__':
    user = session.query(User).filter_by(name=='foo').first()
    user.name = 'bar'
    session.add(user)
    session.commit() #validation not working here

存储新用户时,验证有效 但在更新现有用户时,验证无效

问:如何使用@validates更新其值时验证表列?

谢谢:)

0 个答案:

没有答案