Thanks Michael for looking into my problem. I tried following your advice 
but still not able to get the total_sale. Do I have to implement some 
special getter/setter for memoized property. If you run the code, you will 
see that nowhere  'print "SETTING TOTAL_SALES"' is getting executed which 
is in the memoized definition. Also how can i access the memoized property 
as it is not available in straight forward querying on Artist.

http://pastebin.com/Gqa7b0dd

On Tuesday, 7 August 2012 00:14:07 UTC+5:30, Michael Bayer wrote:
>
> @memoized_property is a handy tool, and works simply, just clear out 
> __dict__ of that key and it's reset.
>
> since you're looking to work in python, the total sales are just:
>
>     @memoized_property
>     def total_sales(self):
>         # This should be the sum of all Album sales for this Artist. It 
> should
>         # be updated as soon as/just after a new Album is added or an 
> Album's
>         # sales is updated.
>         return sum([album.sales or 0 for album in self.albums])
>
> the events you need are simple, just whenever "sales" or "artist" (ideally 
> you'd name this in the singular since it is many-to-one) change, pop the 
> "total_sales" out of the dict:
>
> from sqlalchemy.orm import validates
>
> class Album(Base):
>     # ...
>     @validates("sales")
>     def _update_sales(self, key, value):
>         if self.artist is not None:
>             self.artist.__dict__.pop('total_sales', None)
>         return value
>
>     @validates("artist", include_removes=True)
>     def _update_artist(self, key, artist, is_remove):
>         artist.__dict__.pop('total_sales', None)
>         return artist
>
>
>
> On Aug 6, 2012, at 4:08 AM, jeetu wrote:
>
> My problem scenario is analogous to the following. I have an Artist table 
> and an Album table. Each artist can have multiple albums with sales of each 
> album. The artist also has a total_sales column which is basically a 
> cumulative of album's sales for that artist. I tried reading about 
> attribute events and memoized property but I am unable to get it integrated 
> with my code.  I tried to achieve something like 
> https://groups.google.com/forum/?fromgroups#!topic/sqlalchemy/o_KxuHwz4WQ. 
> But my lack of thorough understanding of decoraters and sqlalchemy is 
> proving to be a hindrance. My example code (heavily borrowed from resources 
> on internet and sqlalchemy group) is http://pastebin.com/vhRTcrWV 
> Just for information if at all it matters:n my actual code I am using 
> toscawidgets (inside turbogears) and dynamic forms for creating Albums 
> table's values and Artist's values on a single page. 
>
> PS: I hope it is not sounding like homework 
>
>
>
> -- 
> 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/-/CcGMUZ2UHSYJ.
> 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.
>
>
>

-- 
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/-/co2-P80LasQJ.
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.

Reply via email to