考虑下面的一对多模型。
class Person(Base):
id
things = relationship('Thing')
def some_magic_here():
pass
class Thing(Base):
id
person_id = ForeignKey(person.id)
我想要实现的是,当我做以下事情时:
p = Person()
thing1 = Thing()
thing2 = Thing()
p.things = [thing1, thing2]
方法some_magic_here
将在将thing1和thing2作为子对象添加之前执行一些处理工作
您需要事件系统:http://docs.sqlalchemy.org/en/latest/orm/events.html#sqlalchemy.orm.events.AttributeEvents.append
例如,类似于:
相关问题 更多 >
编程相关推荐