【发布时间】:2014-10-23 13:26:29
【问题描述】:
我有 4 张桌子:
class Cebola(Base):
__tablename__ = 'cebolas'
id = Column(Integer, primary_key=True, autoincrement=True)
class Checklist(Base):
__tablename__ = 'checklists'
id = Column(Integer, primary_key=True, autoincrement=True)
checklist_type = Column(String)
cebola_id = Column(Integer, ForeignKey('cebolas.id'))
cebola = relationship('Cebola', backref=backref('checklists'))
__mapper_args__ = {'polymorphic_on': checklist_type,
'polymorphic_identity': 'generic'}
class ChecklistA(Checklist):
__tablename__ = 'checklist_a'
id = Column(Integer, ForeignKey('checklists.id', ondelete='CASCADE'), primary_key=True)
notes = Column(Unicode)
__mapper_args__ = {'polymorphic_identity': 'a'}
class ChecklistB(Checklist):
__tablename__ = 'checklist_b'
id = Column(Integer, ForeignKey('checklists.id', ondelete='CASCADE'), primary_key=True)
notes = Column(Unicode)
__mapper_args__ = {'polymorphic_identity': 'b'}
现在我需要一种方法(可能是混合属性),它可以告诉我在 notes <> '' 的 Cebola 中有多少清单。
我已添加:
class Cebola(Base):
@hybrid_property
def number_of_comments(self):
return len([c for c in self.checklists if c.notes])
@number_of_comments(cls)
def number_of_comments(cls):
???
我在SQLAlchemy - Writing a hybrid method for child count 中发现了类似的问题,但我的示例稍微复杂一些。
【问题讨论】:
-
您希望如何处理没有
notes属性的Checklist泛型实例? -
@van 他们可以忽略
标签: python sqlalchemy