On Jan 5, 2012, at 3:03 PM, Mariano Mara wrote: > Hi there! I have a master-detail entity. Since I do some post-processing work > on the details before inserting the entity in the db, I added an > 'after_insert' event where I do the extra stuff. > One of the things I need is to make sure certain "details" have been > selected by the user and in case he didn't I add some default values on his > behalf, just like > > master.detail.append(DetailCls(score=100, > timestamp=datetime.datetime.now()))
don't do any kind of manipulation of the flush plan, which includes collection alterations, inside of after_insert(). The append() operation here is most cleanly implemented in the __init__ method of your Master object. That way the model itself handles the shape it should be in. Another way to set up state is using the @validates hook to respond to an attribute set event: http://www.sqlalchemy.org/docs/orm/mapper_config.html?highlight=validates#simple-validators The other way is to use the before_flush event: http://www.sqlalchemy.org/docs/orm/events.html?highlight=before_flush#sqlalchemy.orm.events.SessionEvents.before_flush -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.