[Zope] object creation in a specific folder...
J Cameron Cooper
zope-l at jcameroncooper.com
Mon Apr 11 17:38:30 EDT 2005
Rushabh Mehta wrote:
> hello all,
>
> this is my first zope app... i am trying to figure out how to create
> an object in a particular folder via script, when my current object
> may not be in the right tree...
>
> I tried to do a 'change folder' via restrictedTraverse (eg
> context.getRestrictedTraverse('/myapp/myfolder'), and then create the
> object using manage_addProduct... but it dosnt seem to work. any tips?
The manage_addProduct method creates an object in the context in which
is was called. Which is to say, the object it is called on. If you say
context.manage_addProduct(...) you're creating an object on the current
object (or the current object's container.)
The restrictedTraverse method gets and returns some object; it does not
"change folders". (The API note reads: "Return the object obtained by
traversing the given path from the object on which the method was
called, performing security checks along the way.") You're thinking
file-system-wise, and that doesn't work here. Think objects.
(Note: I don't think 'getRestrictedTraverse' exists.
'restrictedTraverse' on the other hand, definitely does.)
What you want to do, then, is call manage_addProduct on the object
returned by restrictedTraverse::
context.restrictedTraverse('/wherever/a').manage_addProduct(...)
Of course, you can also get a certain folder via acquisition::
context.wherever.a.manager_addProduct(...)
unless you're getting paths dynamically.
--jcc
More information about the Zope
mailing list