[Zope] Storing and Using Object references

Tres Seaver tseaver@digicool.com
Thu, 11 Jan 2001 22:46:19 -0500


Tom Jenkins <tjenkins@devis.com> wrote
> 
> Hello all,
> i have a question and I hope someone can point me in the right direction
> to solve it.  I need one zope object to hold a reference to another zope
> object so the first object can call methods of the second object.  oh,
> these are python classes.  
> 
> Example: object 1 is :  /container1/container1a/item1  object 2 is :
> /container2/container2a/item1  object1 needs to hold a reference to
> object2.  I'm really stuck on how to store and access object2 from
> object1.  
> 
> any pointers?

If you store the path to the other object as an attribute on its
referer, then you can use the OFS.Traversal interface to fetch the
referred object on demand.  E.g.::

  class Foo:

    bar_path = None

    def setBar( self, bar ):
        """
            Save bar's path so we can find it later.
        """
        if bar is None:
            self.bar_path is None
        else:
            self.bar_path = bar.getPhysicalPath()

    def getBar( self ):
        """
            Use stored path to retrieve bar.
        """
        if self.bar_path is None:
            return None
        return self.restrictedTraverse( self.bar_path )
        #            ^_could be 'unrestrictedTraverse', in trusted code
      
Tres
-- 
===============================================================
Tres Seaver                                tseaver@digicool.com
Digital Creations     "Zope Dealers"       http://www.zope.org