summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryelite <yelite958@gmail.com>2014-05-07 23:39:56 +0800
committeryelite <yelite958@gmail.com>2014-05-08 08:55:17 +0800
commit106adc29681050ce0ab31bdf71a63855b10ae2b0 (patch)
treed29ef211ac0be572c5b97fe3adea9332f9ad3774
parentc8873b31f0c87ba0d1a7518b36af7151dec34be4 (diff)
downloadsqlalchemy-pr/86.tar.gz
add reflective parameter to AssociationProxypr/86
When reflective is set to True the object which owns this proxy will be passed to creator
-rw-r--r--lib/sqlalchemy/ext/associationproxy.py16
1 files changed, 14 insertions, 2 deletions
diff --git a/lib/sqlalchemy/ext/associationproxy.py b/lib/sqlalchemy/ext/associationproxy.py
index 045645f86..9f29d362b 100644
--- a/lib/sqlalchemy/ext/associationproxy.py
+++ b/lib/sqlalchemy/ext/associationproxy.py
@@ -93,7 +93,7 @@ class AssociationProxy(interfaces._InspectionAttr):
def __init__(self, target_collection, attr, creator=None,
getset_factory=None, proxy_factory=None,
- proxy_bulk_set=None):
+ proxy_bulk_set=None, reflective=False):
"""Construct a new :class:`.AssociationProxy`.
The :func:`.association_proxy` function is provided as the usual
@@ -137,6 +137,11 @@ class AssociationProxy(interfaces._InspectionAttr):
:param proxy_bulk_set: Optional, use with proxy_factory. See
the _set() method for details.
+ :param reflective: Optional. When set to ``True`` the object which owns
+ this proxy will be passed to `creator`. For list and set collections,
+ the `creator` will be called with object and value. For dict types,
+ three arguments are passed: object, key and value.
+
"""
self.target_collection = target_collection
self.value_attr = attr
@@ -144,6 +149,7 @@ class AssociationProxy(interfaces._InspectionAttr):
self.getset_factory = getset_factory
self.proxy_factory = proxy_factory
self.proxy_bulk_set = proxy_bulk_set
+ self.reflective = reflective
self.owning_class = None
self.key = '_%s_%s_%s' % (
@@ -447,12 +453,16 @@ class _lazy_collection(object):
self.target = target
def __call__(self):
+ return getattr(self.obj, self.target)
+
+ @property
+ def obj(self):
obj = self.ref()
if obj is None:
raise exc.InvalidRequestError(
"stale association proxy, parent object has gone out of "
"scope")
- return getattr(obj, self.target)
+ return obj
def __getstate__(self):
return {'obj': self.ref(), 'target': self.target}
@@ -489,6 +499,8 @@ class _AssociationCollection(object):
"""
self.lazy_collection = lazy_collection
+ if parent.reflective:
+ creator = lambda *args: creator(self.lazy_collection.obj, *args)
self.creator = creator
self.getter = getter
self.setter = setter