次のような 2 つのモデルを作成しました。
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')
ここで、1 つの ItemType が複数の ItemTypeComments を持つことができることがわかります。ここで、クラス ItemType にメソッドを追加して、コメントを簡単に追加したいと考えています。現在、私は書いた:
def add_comment(self, comment):
comment = ItemTypeComment()
comment.comment = comment
comment.itemtype = self
DBSession.add(comment)
これを行うより良い方法があるかどうか知りたいですか?ありがとう。