summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/ext/hybrid.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2011-12-05 17:25:53 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2011-12-05 17:25:53 -0500
commita582c987d7fb714cc24abc7bba59c536a145c8d8 (patch)
treeacfcde77b67ddc9e96b2f85efdd1f58154699278 /lib/sqlalchemy/ext/hybrid.py
parent54a72583b4001b8aac113236898466c7f1f9c238 (diff)
downloadsqlalchemy-a582c987d7fb714cc24abc7bba59c536a145c8d8.tar.gz
refine this a bit
Diffstat (limited to 'lib/sqlalchemy/ext/hybrid.py')
-rw-r--r--lib/sqlalchemy/ext/hybrid.py22
1 files changed, 18 insertions, 4 deletions
diff --git a/lib/sqlalchemy/ext/hybrid.py b/lib/sqlalchemy/ext/hybrid.py
index 31732b93b..02a3d7d53 100644
--- a/lib/sqlalchemy/ext/hybrid.py
+++ b/lib/sqlalchemy/ext/hybrid.py
@@ -489,16 +489,30 @@ using :attr:`.Operators.eq` against the left and right sides, passing into
We can modify the pattern to be more verbose but flexible by separating
the "join" step from the "filter" step. The tricky part here is ensuring
that successive instances of ``GrandparentTransformer`` use the same
-:class:`.AliasedClass` object against ``Node`` - we put it at the
-class level here but other memoizing approaches can be used::
+:class:`.AliasedClass` object against ``Node``. Below we use a simple
+memoizing approach that associates a ``GrandparentTransformer``
+with each class::
+
+ class Node(Base):
+
+ # ...
+
+ @grandparent.comparator
+ def grandparent(cls):
+ # memoize a GrandparentTransformer
+ # per class
+ if '_gp' not in cls.__dict__:
+ cls._gp = GrandparentTransformer(cls)
+ return cls._gp
class GrandparentTransformer(Comparator):
- parent_alias = aliased(Node)
+
+ def __init__(self, cls):
+ self.parent_alias = aliased(cls)
@property
def join(self):
def go(q):
- expression = self.__clause_element__()
return q.join(self.parent_alias, Node.parent)
return go