@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