1. 程式人生 > >SQLAlchemy-Utils,提供choice功能

SQLAlchemy-Utils,提供choice功能

() pes ins pre true incr pip blog bsp

SQLAlchemy操作數據庫建表時,無法像Django一樣提供choice方法,我們開頭導入SQLAlchemy-Utils來為我們提供這個功能

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)

SQLAlchemy-Utils,提供choice功能