我有一个类Alert
,它的类别是多态的:
class Alert(Base):
__tablename__ = "alert"
name = Column(Unicode)
category = Column(Unicode)
frequency = Column(Unicode)
__mapper_args__ = {'polymorphic_on': category}
Alert
有子类TopicAlert
和SearchAlert
,它们有自己的get_results
方法:
class TopicAlert(Alert):
__mapper_args__ = {"polymorphic_identity": "Topic Alert"}
def get_results(self):
# code here
class SearchAlert(Alert):
__mapper_args__ = {"polymorphic_identity": "Search Alert"}
def get_results(self):
# code here
每当我添加一个新警报时,我想获得结果并将其保存到另一个表中,我称之为AlertResult
:
def add_alert(request):
alert = Alert()
alert.name = request.params.get('name')
alert.category = request.params.get('category')
alert.frequency = request.params.get('frequency')
Session.add(alert)
Session.flush()
alert_result = AlertResult()
alert_result.alert = alert
alert_result.result = alert.get_results()
Session.add(alert_result)
Session.flush()
但我收到错误:
AttributeError: 'Alert' object has no attribute 'get_results'
如果我向get_results()
添加Alert
方法,则会始终执行此方法。是不是可以在SQLAlchemy中为子类定义自定义方法/覆盖继承方法?