diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-09-07 20:28:19 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-09-07 20:28:19 -0400 |
commit | 390207e533854ab0c3abe6b7ebc45fae1b14eaba (patch) | |
tree | 435ab97d2cf1656eb5df2984d72fafdf2a364a1b /lib/sqlalchemy/orm/attributes.py | |
parent | 083d44c082a13761910c761debd17efac75a71b3 (diff) | |
download | sqlalchemy-390207e533854ab0c3abe6b7ebc45fae1b14eaba.tar.gz |
- Added new event handlers :meth:`.AttributeEvents.init_collection`
and :meth:`.AttributeEvents.dispose_collection`, which track when
a collection is first associated with an instance and when it is
replaced. These handlers supersede the :meth:`.collection.linker`
annotation. The old hook remains supported through an event adapter.
Diffstat (limited to 'lib/sqlalchemy/orm/attributes.py')
-rw-r--r-- | lib/sqlalchemy/orm/attributes.py | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/lib/sqlalchemy/orm/attributes.py b/lib/sqlalchemy/orm/attributes.py index 66197ba0e..459a52539 100644 --- a/lib/sqlalchemy/orm/attributes.py +++ b/lib/sqlalchemy/orm/attributes.py @@ -863,6 +863,16 @@ class CollectionAttributeImpl(AttributeImpl): self.copy = copy_function self.collection_factory = typecallable + if hasattr(self.collection_factory, "_sa_linker"): + + @event.listens_for(self, "init_collection") + def link(target, collection, collection_adapter): + collection._sa_linker(collection_adapter) + + @event.listens_for(self, "dispose_collection") + def unlink(target, collection, collection_adapter): + collection._sa_linker(None) + def __copy(self, item): return [y for y in collections.collection_adapter(item)] @@ -955,9 +965,14 @@ class CollectionAttributeImpl(AttributeImpl): return user_data def _initialize_collection(self, state): - return state.manager.initialize_collection( + + adapter, collection = state.manager.initialize_collection( self.key, state, self.collection_factory) + self.dispatch.init_collection(state, collection, adapter) + + return adapter, collection + def append(self, state, dict_, value, initiator, passive=PASSIVE_OFF): collection = self.get_collection(state, dict_, passive=passive) if collection is PASSIVE_NO_RESULT: @@ -1026,12 +1041,14 @@ class CollectionAttributeImpl(AttributeImpl): # place a copy of "old" in state.committed_state state._modified_event(dict_, self, old, True) - old_collection = getattr(old, '_sa_adapter') + old_collection = old._sa_adapter dict_[self.key] = user_data collections.bulk_replace(new_values, old_collection, new_collection) - old_collection.unlink(old) + + del old._sa_adapter + self.dispatch.dispose_collection(state, old, old_collection) def _invalidate_collection(self, collection): adapter = getattr(collection, '_sa_adapter') |