假设我正在使用如下声明性对象:
class MyObject(DeclarativeBase):
...
other_object_id = Column(Integer, ForeignKey('other_object.id'))
other_object = relationship("OtherObject")
...
假设我想要一个方便的方法set_other_object_value
,如果需要,它将修改other_object.value
、创建一个实例OtherObject
并将其添加到会话中:
def set_other_object_value(self, value):
if self.other_object is None:
<create instance of OtherObject and associate with the session>
self.other_object.value = value
问题是:如何创建OtherObject
并将其与会话关联?一个可能等效的问题:是否可以访问从 的实例中添加的 aSession
的MyObject
实例MyObject
?