Mike, thanks for the response.

(1) "foo" updates a particular User attribute based on a calculation 
performed on the user.courses collection. I'm listening for the "set" event 
on UserCourse objects to trigger "foo" to update that User attribute, but 
that isn't working with new users because -- as you say -- the "set" event 
is called before the actual attribute association occurs. What is the first 
event I could listen for that would recognize the new attribute association?

(2) Re: defaultdict capabilities... There are other patterns I could use to 
first test if a key exists in the collection, but I was looking for a Mike 
Bayer implementation because it'd probably be better...grin.

Sidenote:
In my application, "foo" is actually "set_user_max_interest_distance" 
(http://pastebin.com/SMH1n9Fp), which calculates a value used to normalize 
some other values, but I thought it'd be easier to take the focus off the 
function itself in order to troubleshoot the event sequencing.

-Brian


On Wednesday, July 2, 2014 1:43:59 PM UTC-4, Michael Bayer wrote:
>
>
> 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.

Reply via email to