我正在尝试在具有多个继承的python类上使用多态性:
engine = create_engine(
'mysql://xxx:yyy@localhost:3306/zzz?charset=utf8&use_unicode=0',
pool_recycle=3600, echo=True)
Base = declarative_base()
class AbstractPersistent(object):
version = Column('VERSION', Integer)
last_modified_by = Column('LAST_MODIFIED_BY', String(255))
last_modified_date = Column('LAST_MODIFIED_DATE', Date)
created_by = Column('CREATED_BY', String(255))
created_date = Column('CREATED_DATE', Date)
class AbstractNamed(AbstractPersistent):
eid = Column('ENTERPRISE_ID', String(255))
title = Column('TITLE', String(255))
description = Column('DESCRIPTION', String(255))
class AbstractContainer(AbstractNamed):
__tablename__ = 'CM_MEMBER_CONTAINER_T'
id = Column('MEMBER_CONTAINER_ID',Integer,primary_key=True)
discriminator = Column('CLASS_DISCR', String(100))
__mapper_args__ = {'polymorphic_on': discriminator }
class CourseSet(Base,AbstractContainer):
__mapper_args__ = {'polymorphic_identity': 'org.sakaiproject.coursemanagement.impl.CourseSetCmImpl'}
正如你所看到的,
CourseSet
包含其父级的所有列并具有该列
CLASS_DISCR
集合到
'org.sakaiproject.coursemanagement.impl.CourseSetCmImpl'
.
但当它对所有
法庭
:
Session = sessionmaker(bind=engine)
session = Session()
course_sets = session.query(CourseSet).all()
print(course_sets)
它返回
'CM_MEMBER_CONTAINER_T'
但是我只想要那些
类别分类
设置为
'org.sakaiproject.coursemanagement.impl.coursesetcmimpl'
有什么想法吗?