Programmatically creating instances of a ZClass
I have a Zope Product 'MyProduct' that defines a ZClass 'MyZClass'. I'd like to create instances of this ZClass in a python script. I couldn't find any info on how to do that in 'The Zope Book'. I tried something like: myObject=Container.MyFolder.manage_addProduct['MyProduct'].manage_addMyZClass(id=myId) but it didn't work. Any help will be appreciated. --Borislav
You can try: container.manage_addProduct['MyProduct'].MyProduct_add() MyProduct_add is the constructor of the ZClass MyProduct Cheers, Nicolas Gouzy On Wednesday 04 September 2002 20:08, Borislav wrote:
I have a Zope Product 'MyProduct' that defines a ZClass 'MyZClass'. I'd like to create instances of this ZClass in a python script. I couldn't find any info on how to do that in 'The Zope Book'. I tried something like:
myObject=Container.MyFolder.manage_addProduct['MyProduct'].manage_addMyZCla ss(id=myId)
but it didn't work.
Any help will be appreciated.
--Borislav
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Gouzy Nicolas wrote:
You can try:
container.manage_addProduct['MyProduct'].MyProduct_add()
MyProduct_add is the constructor of the ZClass MyProduct
How do I pass the object id to this constructor? Seems that it reads it from the REQUEST, but I can't assign to REQUEST in a python script. This code: context.REQUEST['id'] = myObjectId container.manage_addProduct['MyProduct'].MyProduct_add() produces an error: Error Type: TypeError Error Value: object does not support item or slice assignment Am I missing something? --Borislav
Your constructor "MyProduct_add" is a DTML Method, isn't it ? Delete it and replace by a Python Script called "MyProduct_add" ====================================== # MyProduct_add source # ====================================== Parameter List: id, REQUEST=None Code: obj=context.MyProduct.createInObjectManager(id=id, {}) if REQUEST is not None: try: u=context.DestinationURL() except: u=REQUEST['URL2'] REQUEST.RESPONSE.redirect(u+'/manage_workspace') ======================================================= Now, you can write in a Python Script: container.manage_addProduct['MyProduct'].MyProduct_add(id='myBeautifulObject') On Wednesday 04 September 2002 21:14, Borislav wrote:
Gouzy Nicolas wrote:
You can try:
container.manage_addProduct['MyProduct'].MyProduct_add()
MyProduct_add is the constructor of the ZClass MyProduct
How do I pass the object id to this constructor? Seems that it reads it from the REQUEST, but I can't assign to REQUEST in a python script. This code:
context.REQUEST['id'] = myObjectId container.manage_addProduct['MyProduct'].MyProduct_add()
produces an error:
Error Type: TypeError Error Value: object does not support item or slice assignment
Am I missing something?
--Borislav
-- Nicolas Gouzy Chef de Projet / Ingénieur de Développement OPIXIDO 9, cité paradis 75010 Paris FRANCE tel : 01 40 22 92 46 ou 47 fax : 01 40 22 92 44
Gouzy Nicolas wrote:
Your constructor "MyProduct_add" is a DTML Method, isn't it ? Delete it and replace by a Python Script called "MyProduct_add"
====================================== # MyProduct_add source # ======================================
Parameter List: id, REQUEST=None
Code: obj=context.MyProduct.createInObjectManager(id=id, {})
if REQUEST is not None: try: u=context.DestinationURL() except: u=REQUEST['URL2'] REQUEST.RESPONSE.redirect(u+'/manage_workspace')
=======================================================
Now, you can write in a Python Script: container.manage_addProduct['MyProduct'].MyProduct_add(id='myBeautifulObject')
Zope has a problem with the first line: obj=context.MyProduct.createInObjectManager(id=id, {}) When I try to save the code I'm getting an error: Zope has encountered an error while publishing this resource. Error Type: SyntaxError Error Value: non-keyword arg after keyword arg When I comment out the first line, Zope accepts the code. Any suggestions? --Borislav
Sorry, write: obj=context.MyProduct.createInObjectManager(id, {}) On Thursday 05 September 2002 16:59, Borislav wrote:
Gouzy Nicolas wrote:
Your constructor "MyProduct_add" is a DTML Method, isn't it ? Delete it and replace by a Python Script called "MyProduct_add"
====================================== # MyProduct_add source # ======================================
Parameter List: id, REQUEST=None
Code: obj=context.MyProduct.createInObjectManager(id=id, {})
if REQUEST is not None: try: u=context.DestinationURL() except: u=REQUEST['URL2'] REQUEST.RESPONSE.redirect(u+'/manage_workspace')
=======================================================
Now, you can write in a Python Script: container.manage_addProduct['MyProduct'].MyProduct_add(id='myBeautifulObj ect')
Zope has a problem with the first line: obj=context.MyProduct.createInObjectManager(id=id, {})
When I try to save the code I'm getting an error:
Zope has encountered an error while publishing this resource. Error Type: SyntaxError Error Value: non-keyword arg after keyword arg
When I comment out the first line, Zope accepts the code. Any suggestions?
--Borislav
-- Nicolas Gouzy Chef de Projet / Ingénieur de Développement OPIXIDO 9, cité paradis 75010 Paris tel : 01 40 22 92 46 ou 47 fax : 01 40 22 92 44
Ok, we are on the right track here... After I create the object with: container.manage_addProduct['MyProduct'].MyProduct_add(id='myBeautifulObject') I can get the object id with: container.objectIds() However, the object does not appear if I browse the container through the ZMI. Is there an additional step, maybe setting some object properties, so that the object appears in the ZMI? Gouzy Nicolas wrote:
Sorry, write: obj=context.MyProduct.createInObjectManager(id, {})
Borislav writes:
After I create the object with:
container.manage_addProduct['MyProduct'].MyProduct_add(id='myBeautifulObject')
I can get the object id with:
container.objectIds()
However, the object does not appear if I browse the container through the ZMI. Is there an additional step, maybe setting some object properties, so that the object appears in the ZMI? No. But, there must be no error (exception) after the object is created. Otherwise, the transaction is aborted and all of its effects wiped out.
Another solution may be, that "container" is not what you expect. In this case, the objects would be created elsewhere.. Dieter
Dieter Maurer wrote:
No. But, there must be no error (exception) after the object is created. Otherwise, the transaction is aborted and all of its effects wiped out.
That was it. I was testing with raise 'Debug', myValue after the object was created, so the transaction was never commited. Now it works. Thanks for the valuable input. --Borislav
participants (3)
-
Borislav -
Dieter Maurer -
Gouzy Nicolas