我写了两个这样的模型:
class ItemType(Base, DBBase):
__tablename__ = 'itemtypes'
id = Column(Integer, primary_key = True)
name = Column(String), nullable = True)
comments = relationship('ItemTypeComment')
class ItemTypeComment(Base, DBBase):
__tablename__ = 'itemtypecomments'
id = Column(Integer, primary_key = True)
comment = Column(String), nullable = True)
itemtype_id = Column(Integer, ForeignKey('itemtypes.id'), nullable = True)
itemtype = relationship('ItemType')
这里我们可以看到,一个 ItemType 可以有多个 ItemTypeComments。现在我想向 ItemType 类添加一个方法,以便轻松地向它添加注释。目前,我写道:
def add_comment(self, comment):
comment = ItemTypeComment()
comment.comment = comment
comment.itemtype = self
DBSession.add(comment)
我想知道是否有更好的方法来做到这一点?谢谢。