[ZPT] Best way of getting the first results from ZSQL queries

Casey Duncan casey@zope.com
Thu, 5 Sep 2002 23:59:11 -0400


You'd be much better off coding this in a Python script that either retur=
ned=20
the record (or None) or set the global variable into the request.

something like:

form =3D context.REQUEST.form
if form.has_key('autoid'):
    data =3D container.get_teaching(autoid=3Dform['autoid'])
    if data:
        return data[0]
return None # You can omit this line, but it makes it clear

Let's say this script is called "get_form_data" then in your template put=
=20
something like:

<body tal:define=3D"form_data container/get_form_data">
  ...Do Stuff..
  <div tal:condition=3D"form_data">
    ..do stuff with form_data..
  </div>
</body>

hth,

-Casey

On Thursday 05 September 2002 01:28 am, Tony McDonald wrote:
> Hi all,
> I have a situation that requires I show a form ready for data to be ent=
ered
> or populate that form with data that will be edited.
>=20
> Whether the form is populated or not depends on whether a variable (aut=
oid)
> is passed to the template.
>=20
> What I have is this. It
> (1) checks whether autoid exists
> (2) does the ZSQL query (it's only ever going to get zero or one row)
> (3) checks to see that it got a row
> (4) if it did, it sets a global variable
>=20
> <tal:autoid_exists condition=3D"exists:request/autoid">
> <tal:formdef define=3D"form_data_list
> python:here.get_teaching(autoid=3Drequest['autoid'])">
>  <tal:if condition=3D"python:len(form_data_list)>0">
>   <tal:call define=3D"global form_data python:form_data_list[0]"/>
>  </tal:if>
> </tal:formdef>
> </tal:autoid_exists>
>=20
> My form elements have things like
> tal:content=3D"form_data/surname | nothing"
> So if form_data does not exist, the whole thing doesn't break.
>=20
> My question is simple - is this the best way to do this, it seems *real=
ly*
> clunky, and I'm loathe to show it to my troops who are just getting int=
o ZPT
> as it would likely make them run away screaming.
>=20
> Any thoughts would be appreciated.
> Tone.
> --=20
> Dr Tony McDonald,  Assistant Director, FMCC, http://www.fmcc.org.uk/
> The Medical School, Newcastle University Tel: +44 191 243 6140
> A Zope list for UK HE/FE  http://www.fmcc.org.uk/mailman/listinfo/zope
>=20
>=20
> _______________________________________________
> ZPT mailing list
> ZPT@zope.org
> http://lists.zope.org/mailman/listinfo/zpt
>=20