At 11:48 AM +0100 12/1/05, Johan Carlsson wrote:
To access an object in the ZODB you need to have a context
(that is a pointer to a object stored in ZODB, it almost alway is but
in some situations it's not! I've learned that the hard way).

If your in a browser class you will have the context of the object
traversed as an instance attribute (self.context).

Ok, this helps, although I'm not completely clear on how context
objects are generated. From a newbie perspective, the context
object is an elusive beast.  It's not really explained anywhere
that I could find. The Zope 3 Developer's Guide. There's a little
section called "The context Variable" in the ZCML chapter, but
that doesn't explain where the context comes from or what it is.

I've seen how the context object is used in TAL, but have always
understood it to be the local URL context.

There were some examples of using self.context in the MessageBoard
tutorial. For instance, below is an excerpt from page 114.
The self.context is used in a mysterious way here. Nothing in
the class definition binds it. It has no base class. I guess I should
expect a child class, or a factory maybe to add the self.context
when the class get instantiated. However, I had trouble finding that
anywhere in the SVN copy of the MessageBoard tutorial (step 13).

class MessageDetails:

    def author(self):
        """Get user who last modified the Message."""
        creators = ICMFDublinCore(self.context).creators
        if not creators:
            return _('unknown')
        return creators[0]

    def modified(self):
        """Get last modification date."""
        date = ICMFDublinCore(self.context).modified
        if date is None:
            date = ICMFDublinCore(self.context).created
        if date is None:
            return ''
        formatter = self.request.locale.dates.getFormatter('dateTime', 'short')
        return formatter.format(date)

    def parent_info(self):
        """Get the parent of the message"""
        parent = zapi.getParent(self.context)
        if not IMessage.providedBy(parent):
            return None
        return {'name':, 'title': parent.title}

In some other example MessageBoard classes, context is used
as an init parameter. The doctest examples indicate that
context can be a simple container object, not necessarily something
in ZODB. I guess this makes sense.

I looked around in Philikon's book for info about the context
object as well, but had trouble. It wasn't in the index.
I did see it used as an init parameter in several places, but
had trouble locating which page explained it.

Is use of the word "context" just a naming convention for
the containing (parent) object? That still wouldn't explain the
how self.context works without generating a namespace error
in the MessageDetails class.

Zope3-users mailing list

Reply via email to