Getting a handle on the results of constructors
In an External Method (that is, unrestricted python), for example, when you create a Folder one writes: self.manage_addProduct['OFSP'].manage_addFolder( id,title="folder") one would think that it would return the object it just constructed so it would be easy to use the new product to, say, create another folder or a file inside. But that appears not to be the case. (In my example, the folder was successfully created but the code handle = self.manage_addProduct... returned None. That made what I thought was the obvious way to handle things, namely applying the constructor to the new folder using handle = self.manage_addProduct... fails as the constructor returns None. So, how do you get a reference to the newly created object?
Dennis Allison wrote:
In an External Method (that is, unrestricted python), for example, when you create a Folder one writes:
self.manage_addProduct['OFSP'].manage_addFolder( id,title="folder")
one would think that it would return the object it just constructed so it would be easy to use the new product to, say, create another folder or a file inside. But that appears not to be the case. (In my example, the folder was successfully created but the code
handle = self.manage_addProduct...
returned None. That made what I thought was the obvious way to handle things, namely applying the constructor to the new folder using
handle = self.manage_addProduct...
Here is a short example on how to create a new Folder containing a DTMLMethod Assumed there is a my_html.dtml file in your product directory : import OFS.Folder from OFS.DTMLMethod import DTMLMethod from Globals import HTMLFile dtml_template=HTMLFile('my_html',globals()) dtml_obj=DTMLMethod() dtml_obj.id='my_html' index_obj.manage_edit(data=dtml_template,title='Welcome Home') folder_obj=OFS.Folder.Folder() folder_obj.id='NewFolder' folder_obj._setObject('my_html',index_obj) self._setObject('NewFolder',folder_obj) -- _______________________________________________________________________ Andreas Heckel andreas@easyleading.org
Dennis Allison writes:
In an External Method (that is, unrestricted python), for example, when you create a Folder one writes:
self.manage_addProduct['OFSP'].manage_addFolder( id,title="folder")
one would think that it would return the object it just constructed so it would be easy to use the new product to, say, create another folder or a file inside. But that appears not to be the case. (In my example, the folder was successfully created but the code
handle = self.manage_addProduct...
returned None. That made what I thought was the obvious way to handle things, namely applying the constructor to the new folder using
handle = self.manage_addProduct...
fails as the constructor returns None. So, how do you get a reference to the newly created object? I agree with you, but returning objects is sometimes not so good in a Web context.
You get the object by using "getattr(container,id)". "container" is the container where you created the object, "id" is its id. Dieter
On Fri, 19 Apr 2002, Dieter Maurer wrote:
I agree with you, but returning objects is sometimes not so good in a Web context.
You get the object by using "getattr(container,id)". "container" is the container where you created the object, "id" is its id.
Thank you. I shoulda thought of that...
participants (3)
-
Andreas Heckel -
Dennis Allison -
Dieter Maurer