[Zope3-Users] Direct ZODB access in a Zope3 instance
Johan Carlsson
johanc at easypublisher.com
Thu Dec 1 15:49:13 EST 2005
Brad Allen wrote:
>
>
> 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.
If you really what too know you should read "The life of a Request".
http://www.zope.org/Wikis/DevSite/Projects/ComponentArchitecture/Zope3Book/request.html
Or you can just trust me ;-)
> 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.
Yes that's what it is most of the time, only when you have
view classes (IView) that call each other, for instance
when you use special kind of fields.
Here's the interfaces that define a view:
(zope/component/bbb/interfaces.py)
class IContextDependent(Interface):
"""Components implementing this interface must have a
context component.
Usually the context must be one of the arguments of the
constructor. Adapters and views are a primary example of
context-dependent components.
"""
context = Attribute(
"""The context of the object
This is the object being adapted, viewed, extended, etc.
""")
class IPresentation(Interface):
"""Presentation components provide interfaces to external actors
The are created for requests, which encapsulate external actors,
connections, etc.
"""
request = Attribute(
"""The request
The request is a surrogate for the user. It also provides the
presentation type and skin. It is of type
IPresentationRequest.
""")
So as you see it's not only view that have the context, but adapters
too, well infact view are adapters now day :-)
> 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).
Well, it gets bind when adapted and in this example it occurs
when the object is traversed and it's view is looked up.
I think it gets registered with a view class in ZCML
which turns it in to a browser view
I can agree with you that it's kind of mysterious and implicit,
in fact I think it's a consequence of the aspect-origentation.
(What ever happend to "explicit is better than implicit" ;-)
> 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.
Exactly, from above: "context - The context of the object
This is the object being adapted, viewed, extended, etc."
As it always have been with Zope, the source code is the best way
to understand how it works. Unfortunatelly is the Z3 source
extremely co-located, but still it's very useful to learn to find
ones way in it. After over 6 months hard studying I'm starting
to find my way round the source :-)
Here's some tips:
- ZCML is as much part of the code as anything else, so try to
learn how to find the code behind the ZCML statement.
Specially high level things like ContainerView, AddViews, and
EditView etc.
Ah, can think of any thing else at the moment. I have to give you a
rain-check on that :-)
Regards,
Johan
Regards,
Johan
--
Johan Carlsson Tel: + 46 8 31 24 94
Colliberty Mob: + 46 70 558 25 24
Torsgatan 72 Email: johanc at easypublisher.com
SE-113 37 STOCKHOLM
More information about the Zope3-users
mailing list