Simple validation with SQLAlchemy

狂风中的少年 提交于 2019-12-12 21:15:43

问题


I'm new to sqlalchemy, and I'm trying to achieve simple validation of model's fields, as provided by Django ORM (min & max for Integer, email, ...). Can SQLAlchemy do this sort of field validations out of the box ? By the way, I'm using SQLAlchemy with Flask.


回答1:


See Simple Validators in the documentation. Sample code extract below:

class EmailAddress(Base):
    __tablename__ = 'address'

    id = Column(Integer, primary_key=True)
    email = Column(String)

    @validates('email')
    def validate_email(self, key, address):
        assert '@' in address
        return address


来源:https://stackoverflow.com/questions/8256715/simple-validation-with-sqlalchemy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!