getting at ZCatalog data object in Python
I'd like to return the data object a ZCatalog entry points to so a ZPT can display the data. Currently I'm doing it in a 2-step process where a Python script finds/returns the catalog entry and the ZPT uses getObject to define a variable to get at the actual data. What I want to do is have the Python script find the catalog entry then return the actual data object to the ZPT. CatalogClientTest Script excerpt: # normally uses variables instead of hard-coded values zcat = context.Catalog(datatype='client',id='12346') return zcat ZPT excerpt: <body tal:define="results container/CatalogClientTest"> ... <div tal:define="cdata result/getObject"> ... <span tal:replace="cdata/cl_lastname">client lastname</span> I can get at the data object in the Python script if I hardcode the data_record_id: clientrec = context.Catalog.getobject(833051159) return clientrec but I can't figure out how to get from zcat to clientrec programmatically. Any advice? Thanks, Tom P. Allred Tom@AllredData.com
Tom P. Allred wrote at 2003-11-16 17:06 -0500:
I'd like to return the data object a ZCatalog entry points to so a ZPT can display the data. Currently I'm doing it in a 2-step process where a Python script finds/returns the catalog entry and the ZPT uses getObject to define a variable to get at the actual data. What I want to do is have the Python script find the catalog entry then return the actual data object to the ZPT.
The Python Script can use "getObject" in the same way as the ZPT, e.g. .... return [x.getObject() for x in Catalog(queryspec)] Note, that "getObject" is a potentially expensive operation. You would not like to call it on hundreds/thousands of objects (especially not, when you later use only a handful). -- Dieter
Thanks for the reply. It gave me an error in the ZPT (TypeError: sequence index must be integer ) when I tried to use it as in your example, but worked when I assigned it to a variable and returned that instead. BTW, only one data object will get returned per query. Here is my test ZPT that pulls the client's last name from the data object: <html> <body tal:define="client container/CatalogClientTest2"> <p><b>Client Name: </b> <span tal:replace="client/cl_lastname">ClientName</span> </p> </body> </html> Here is my modified (working) script based on your suggestion: for x in context.Catalog(datatype='client',id='12346'): drec = x.getObject() return drec Just out of curiosity, can you tell me why the following doesn't work (Script Error: LazyMap instance has no attribute 'getObject') since only one catalog item is the result of the query? zcat = context.Catalog(datatype='client',id='12346') drec = zcat.getObject() return drec Thanks, Tom P. Allred, MCSE, RHCE, CCNA Allred Data Consulting, Inc. 888-381-0611 www.AllredData.com Tom@AllredData.com -----Original Message----- From: zope-bounces+tom=allreddata.com@zope.org [mailto:zope-bounces+tom=allreddata.com@zope.org]On Behalf Of Dieter Maurer Sent: Monday, November 17, 2003 1:57 PM To: tom@allreddata.com Cc: zope@zope.org Subject: Re: [Zope] getting at ZCatalog data object in Python Tom P. Allred wrote at 2003-11-16 17:06 -0500:
I'd like to return the data object a ZCatalog entry points to so a ZPT can display the data. Currently I'm doing it in a 2-step process where a Python script finds/returns the catalog entry and the ZPT uses getObject to define a variable to get at the actual data. What I want to do is have the Python script find the catalog entry then return the actual data object to the ZPT.
The Python Script can use "getObject" in the same way as the ZPT, e.g. .... return [x.getObject() for x in Catalog(queryspec)] Note, that "getObject" is a potentially expensive operation. You would not like to call it on hundreds/thousands of objects (especially not, when you later use only a handful). -- Dieter _______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
Tom P. Allred wrote:
Here is my modified (working) script based on your suggestion: for x in context.Catalog(datatype='client',id='12346'): drec = x.getObject() return drec
Just out of curiosity, can you tell me why the following doesn't work (Script Error: LazyMap instance has no attribute 'getObject') since only one catalog item is the result of the query?
zcat = context.Catalog(datatype='client',id='12346') drec = zcat.getObject() return drec
A list with one item is still a list. You call 'getObject' on a catalog result list, which knows of no such method. If you know it to have only one result, you may say zcat = context.Catalog(datatype='client',id='12346') drec = zcat[0].getObject() return drec --jcc -- "My point and period will be throughly wrought, Or well or ill, as this day's battle's fought."
participants (3)
-
Dieter Maurer -
J. Cameron Cooper -
Tom P. Allred