[Product-Developers] safe way to get the parent of an object?

Martijn Pieters mj at zopatista.com
Thu Mar 22 17:03:25 UTC 2012


On Thu, Mar 22, 2012 at 17:53, Maarten Nieber <maarten at usecm.com> wrote:
> I'm using obj.getParentNode() to get the parent of an object.
> However, I now read that this function returns obj.aq_parent, and that this is
> not guaranteed to always be correct.
> Apparently, the safe way is to use obj.aq_inner.aq_parent.
>
> Is this true? And is obj.aq_inner.aq_parent guaranteed to exist?

The following piece of code will always work:

    from Acquisition import aq_inner, aq_parent

    parent = aq_parent(aq_inner(context))

Note that if context.aq_parent works, so does context.aq_inner; both
are defined on objects inheriting from Acquisition.Implicit or
Acquisition.Explicit. But using the functions imported from
Acquisition (as opposed to object methods) guarantees that the calls
also work on a context that is not inheriting from those classes. In
other words, if context is not Acquisition aware, the context is
passed through unchanged.

-- 
Martijn Pieters


More information about the Product-Developers mailing list