[Zope3-Users] Direct ZODB access in a Zope3 instance

Johan Carlsson johanc at easypublisher.com
Thu Dec 1 05:48:43 EST 2005


Hi Brad,
(Sorry if I'm assuming the wrong things about what you want to do, but 
hey that life :-)

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).
The chanse is that if our going to do programmatic things to the ZODB
your most likely to do it in some kind of view class.
I call them "controllers" myself, because they are the heavy-weigh
lifters of Zope 3, it's in the "controllers" it all happens even though
Zope 3 sometimes tries to hide the fact that there even are controllers.


The ZAPI interface gives you several ways to traverse object
from a context:
For instance getParent, (getParents,) getRoot, traverse, (traverseName)

These methods will return an content type object (e.g. a object stored 
in ZODB).


Add an object the the ZODB (programmatically) you first need
an object that implements Persistent and you will probably
what to add it to a container so it should also implement
Location.
(Base classes for that is found here:
    from persistent import Persistent
   from zope.app.location import Location)

As I mention in Zope3 you usually add object to a container, but
not necesarally, but the container garantees that the name and
parent (the attributes defined by ILocation interface) gets
setup correcty, if you add it manually you might need to set
that up your self (I'm doing my ILocation interface dynamically
but that's me, always doing weird stuff ;-).

An object that should be contained should also implement the IContained
interface.

The object it self is just create as you would with any object
or you use a factory (which basically looks the same, but a factory
can be an non-class constructor that create and sets up your object),
to add it to a container you usually use __setitem__ (e.g 
container[name]=your_object)

Also if your adding object without using a container, you would probably 
want to trigger some events (ObjectCreatedEvent, but also the "contained 
event": ObjectAddedEvent (or ObjectMovedEvent)


To learn more about this I suggest you look at zope/app/container.

I hope it wasn't too much :-)

Cheers,
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