考虑以下一对多模型。
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的工作
答案 0 :(得分:0)
您想要事件系统:http://docs.sqlalchemy.org/en/latest/orm/events.html#sqlalchemy.orm.events.AttributeEvents.append
e.g。类似的东西:
@listens_for(Person.things, 'append')
def some_magic_here(target, value, initiator):
# do whatever