python-flask-SQLAlchemy-Utils组件

SQLAlchemy-Utils,提供choice功能
        
        定义:
            # pip3 install sqlalchemy-utils
            from sqlalchemy_utils import ChoiceType

            Base = declarative_base()


            class Xuan(Base):
                __tablename__ = 'xuan'
                types_choices = (
                    (1,'欧美'),
                    (2,'日韩'),
                    (3,'老男孩'),
                )
                id = Column(Integer,primary_key=True,autoincrement=True)
                name = Column(String(64))
                types = Column(ChoiceType(types_choices,Integer()))

                __table_args__ = {
                    'mysql_engine':'Innodb',
                    'mysql_charset':'utf8',
                }
        
        
        
        查询:
            result_list = session.query(Xuan).all()
            for item in result_list:
                    print(item.types.code,item.types.value)

 

posted @ 2018-01-16 11:21  Cool·  阅读(1082)  评论(0编辑  收藏  举报