最近朋友推荐的通过sql生成model的模块很不错,可以减少不少的工作量:https://pypi.python.org/pypi/sqlacodegen/
只需要运行一行,例如:
sqlacodegen postgresql:///some_local_db sqlacodegen mysql+oursql://user:password@localhost/dbname sqlacodegen sqlite:///database.db
生成的model:
# coding: utf-8
from sqlalchemy import BigInteger, Column, DateTime, Integer, String, Text, text
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class CCluster(Base):
__tablename__ = 'c_cluster'
ClusterID = Column(Integer, primary_key=True)
ClusterName = Column(String(255))
ClusterDescription = Column(String(2000))
IsDisabled = Column(Integer, server_default=text("'0'"))