summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm/util.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2017-01-25 11:51:04 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2017-03-16 17:16:49 -0400
commit9974e9a46bdf6c570c650aa911b76c2dcfd9327b (patch)
tree5631c6d247855cb8572d6c634987f23e6c068e0d /lib/sqlalchemy/orm/util.py
parent63a7b2d2d9402b06f9bc7745eed2d98ae9f8b11c (diff)
downloadsqlalchemy-9974e9a46bdf6c570c650aa911b76c2dcfd9327b.tar.gz
Add bulk_replace event, integrate with @validates
Added new attribute event :meth:`.AttributeEvents.bulk_replace`. This event is triggered when a collection is assigned to a relationship, before the incoming collection is compared with the existing one. This early event allows for conversion of incoming non-ORM objects as well. The event is integrated with the ``@validates`` decorator. The ``@validates`` decorator now allows the decorated method to receive objects from a "bulk collection set" operation that have not yet been compared to the existing collection. This allows incoming values to be converted to compatible ORM objects as is already allowed from an "append" event. Note that this means that the ``@validates`` method is called for **all** values during a collection assignment, rather than just the ones that are new. Change-Id: I27f59db008d9e521d31a3e30143d7cd997e4b7b3 Fixes: #3896
Diffstat (limited to 'lib/sqlalchemy/orm/util.py')
-rw-r--r--lib/sqlalchemy/orm/util.py23
1 files changed, 21 insertions, 2 deletions
diff --git a/lib/sqlalchemy/orm/util.py b/lib/sqlalchemy/orm/util.py
index fc0dba5ee..73b0be99c 100644
--- a/lib/sqlalchemy/orm/util.py
+++ b/lib/sqlalchemy/orm/util.py
@@ -91,11 +91,20 @@ def _validator_events(
if include_removes:
def append(state, value, initiator):
- if include_backrefs or not detect_is_backref(state, initiator):
+ if (
+ initiator.op is not attributes.OP_BULK_REPLACE and
+ (include_backrefs or not detect_is_backref(state, initiator))
+ ):
return validator(state.obj(), key, value, False)
else:
return value
+ def bulk_set(state, values, initiator):
+ if include_backrefs or not detect_is_backref(state, initiator):
+ obj = state.obj()
+ values[:] = [
+ validator(obj, key, value, False) for value in values]
+
def set_(state, value, oldvalue, initiator):
if include_backrefs or not detect_is_backref(state, initiator):
return validator(state.obj(), key, value, False)
@@ -108,11 +117,20 @@ def _validator_events(
else:
def append(state, value, initiator):
- if include_backrefs or not detect_is_backref(state, initiator):
+ if (
+ initiator.op is not attributes.OP_BULK_REPLACE and
+ (include_backrefs or not detect_is_backref(state, initiator))
+ ):
return validator(state.obj(), key, value)
else:
return value
+ def bulk_set(state, values, initiator):
+ if include_backrefs or not detect_is_backref(state, initiator):
+ obj = state.obj()
+ values[:] = [
+ validator(obj, key, value) for value in values]
+
def set_(state, value, oldvalue, initiator):
if include_backrefs or not detect_is_backref(state, initiator):
return validator(state.obj(), key, value)
@@ -120,6 +138,7 @@ def _validator_events(
return value
event.listen(desc, 'append', append, raw=True, retval=True)
+ event.listen(desc, 'bulk_replace', bulk_set, raw=True)
event.listen(desc, 'set', set_, raw=True, retval=True)
if include_removes:
event.listen(desc, "remove", remove, raw=True, retval=True)