Right ! Thanks *a lot* Mike, that's what I was trying to achieve. I think I don't need to listen to the "set" event since _AssociationSet does not support item assignment but it's a detail. Also, the attribute events don't intercept the creation of an initially empty set (poll.result_values_by_user[joe] = set() ) but it's not an issue either since the default value of vote_dt takes care of this.
Besides, yeah it would be awesome to beef up the @validates decorator cause it would make this kind of code easier to write. On a side note, when validators are bound to a collection, the validator fires for *every* modified item - do you think it could be possible in the future to have a validator which executes only once per collection modification, even if 50 rows for instance are appended at the same time ? Thanks again for your support ! Franck Le mercredi 11 avril 2012 18:44:24 UTC+2, Michael Bayer a écrit : > > > On Apr 11, 2012, at 12:22 PM, Franck wrote: > > > Hi Mike, > > > > Thanks for your prompt answer ! > > > > Thanks for fixing my "default" field - makes complete sense. > > Besides I added the "vote_dt" assignment in the PollVote constructor but > it seems that this constructor is called only once, i.e. after the first > "result_values_by_user" assignment : poll.result_values_by_user[joe] = > set(["A", "B", "C"]) > > > > It seems that the 2 further "result_values_by_user" assignments don't > trigger the constructor - please see the SQL trace below. What I'm trying > to achieve is to automatically update this date not only at row creation > time, but also when the underlying collection is modified. > > > > Am I missing something here ? > > OK so look at the terminiology - "update this date" - you're looking for > an UPDATE to the row, so an __init__ does not correspond to that. > Sometimes we do on-updates at the SQL level using "onupdate", but that's > to respond to something else being updated. In this case, you want the > date on POLL_VOTES updated whenever its related POLL_RESULTS entries change > as well (right ?) - so to get every kind of change on an attribute we can > use attribute events: > > from sqlalchemy import event > > @event.listens_for(PollVote.results, "append") > @event.listens_for(PollVote.results, "remove") > @event.listens_for(PollVote.results, "set") > def _on_vote(target, value, initiator): > target.vote_dt = datetime.datetime.now() > > > I think I'm going to beef up the @validates decorator, which is a > convenience decorator for the above, to include the ability to intercept > deletes. > > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/azNySHau1HUJ. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
