]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
refine this a bit
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 5 Dec 2011 22:25:53 +0000 (17:25 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 5 Dec 2011 22:25:53 +0000 (17:25 -0500)
lib/sqlalchemy/ext/hybrid.py

index 31732b93b39f21fe298436cb1d0a46c18edb9d05..02a3d7d5302c0c83c27e6c43230ee510fcf3364c 100644 (file)
@@ -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