Getting select contents from parent object
Hello, I have an object with properties, and I would like to add to it a select-box as a property. However, I want the possible selections to be defined by a containing object. That sounds sort of confusing, so let me provide an example: In my Zope instance, I have a folderish object, 'apples'. The 'apples' folder has a function, 'list_apple_types', which returns a list: ['good', 'bad', 'red', 'granny']. Now I want to create an apple object and put it in this folder. When I create the apple object, I want to be able to give it a select property where the function that returns the list of possible selections is the 'list_apple_types' folder of the parent folder. The reason I want to do it this way is so that I only have to define that list once, and if I add a new type of apple to the list ('rotten'), then I don't have to go through and add this new possible select value to all of the apple objects in the apples folder. Can anyone tell me how to do this? I have been playing around and reading the docs, but I can't find a way to do this. Thanks very much, VanL
Hi VanL, if you start defining your zclass, simply put the name of the function into the "value" field for property type "select" This is all you need. If you want to try out this while authoring the zclass, you have to move the method to a place, where it can be seen from Control_Panel/Products, like / (root) Happy zoping Tino --On Mittwoch, 7. August 2002 08:27 -0600 VanL <Van@Lindbergs.org> wrote:
Hello,
I have an object with properties, and I would like to add to it a select-box as a property. However, I want the possible selections to be defined by a containing object. That sounds sort of confusing, so let me provide an example:
In my Zope instance, I have a folderish object, 'apples'. The 'apples' folder has a function, 'list_apple_types', which returns a list: ['good', 'bad', 'red', 'granny']. Now I want to create an apple object and put it in this folder. When I create the apple object, I want to be able to give it a select property where the function that returns the list of possible selections is the 'list_apple_types' folder of the parent folder.
The reason I want to do it this way is so that I only have to define that list once, and if I add a new type of apple to the list ('rotten'), then I don't have to go through and add this new possible select value to all of the apple objects in the apples folder.
Can anyone tell me how to do this? I have been playing around and reading the docs, but I can't find a way to do this. Thanks very much,
VanL
_______________________________________________ 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 )
Thanks for the help. However, I am afraid that I'm not doing these as ZClasses, but python products. I havesome initialization code like: thisApple = Apple.Apple(id) self._setObject(id, thisApple) thisApple = self._getOb(id) Now I would like to do something like: PropertyManager.manage_addProperty(thisApple, 'apple_type', [parent].list_apple_types, 'select', None) (all of the above on one line if it got wrapped) It is referencing the [parent].list_apple_types function during (or shortly after) object creation that I can't figure out how to do. Thanks, VanL Tino Wildenhain wrote:
Hi VanL,
if you start defining your zclass, simply put the name of the function into the "value" field for property type "select" This is all you need.
If you want to try out this while authoring the zclass, you have to move the method to a place, where it can be seen from Control_Panel/Products, like / (root)
Happy zoping
Tino
Here is my try: In the Apple Object, I have the following method defined: def get_apple_type(self): typelist = ['No apple types defined'] for parentObj in self.aq_chain: try: typelist = parentObj.typelist return typelist except: continue return typelist I then do the following when I create the object: a = Apple.Apple(thisID) self._setObject(thisID, a) a = self._getOb(thisID) # Now try to set the typelist property PropertyManager.manage_addProperty(a, 'apple_type', a.get_apple_type, 'select', None) I can refresh the product fine, but when I try to add an apple, I get the following error: *Error Type: UnpickleableError* *Error Value: Cannot pickle objects * The traceback is as follows: ** Traceback (innermost last): File C:\lib\ZopeDev\lib\python\ZPublisher\Publish.py, line 150, in publish_module File C:\lib\ZopeDev\lib\python\ZPublisher\Publish.py, line 114, in publish File C:\lib\ZopeDev\lib\python\Zope\__init__.py, line 159, in zpublisher_exception_hook File C:\lib\ZopeDev\lib\python\ZPublisher\Publish.py, line 102, in publish File C:\lib\ZopeDev\lib\python\Zope\__init__.py, line 173, in commit File C:\lib\ZopeDev\lib\python\ZODB\Transaction.py, line 234, in commit File C:\lib\ZopeDev\lib\python\ZODB\Connection.py, line 346, in commit (Info: (('Products.OrchardManager.AppleInfo', 'Apple'), '\x00\x00\x00\x00\x00\x00#\xa8', '')) UnpickleableError: (see above) Any help? Thanks, VanL * * * *
VanL wrote:
I then do the following when I create the object:
a = Apple.Apple(thisID) self._setObject(thisID, a) a = self._getOb(thisID) # Now try to set the typelist property PropertyManager.manage_addProperty(a, 'apple_type', a.get_apple_type, 'select', None)
Surely the above should be: a.manage_addProperty('apple_type',a.get_apple_type(),'select')
I can refresh the product fine, but when I try to add an apple, I get the following error:
*Error Type: UnpickleableError* *Error Value: Cannot pickle objects
well, I reckon that may have been 'cos you actually ended up adding the get_apple_type method object as an attribute of your apple rather than the result of calling that method (see my added brackets in the above code). Method objects aren't pickleable, which hopefully explains your error. cheers, Chris
participants (3)
-
Chris Withers -
Tino Wildenhain -
VanL