On 7/2/14, 11:15 AM, Brian Findlay wrote: > I've since added an event listener to perform a calculation each time > a UserCourse object is set: > > > # Recalculate 'bar' after updating UserCourse > @event.listens_for(UserCourse.grade, 'set') > def foo(target, value, oldvalue, initiator): > courses = DBSession.query(Course).all() > user = User.from_id(target.user_id) > bar = 0 > for course in courses: > bar += user.courses[course.title] > user.bar = bar > > > Here, 'bar' is some calculation involving a user's grade for each > course. This is a somewhat contrived model (my application isn't > really about courses and grades), but I thought it'd help to simplify > my use case. > > There are no issues when a user, the courses, and the user's grades > already exist in the database. However, when a new user submits a form > with course grades in it, the 'foo' function is triggered and I get > > AttributeError: 'NoneType' object has no attribute 'courses'
well it's probably related to the fact that the "set" event is called before the actual attribute association occurs, perhaps some reentrant attribute case, not sure. I'm not sure what the purpose of "foo" is or how it relates to the problem stated. If the desired feature is "defaultdict" capabilities, that means, you want to have the "get" feature of the association proxy to have special behavior. It seems like you'd want to subclass AssociationDict to add that feature. -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.