Using db field labels for column headers

23 views
Skip to first unread message

villas

unread,
Sep 10, 2009, 12:43:22 PM9/10/09
to web2py-users
It seems strange that the db.field 'label' is not used as the column
header by default in SQLTABLE. After all, if someone has gone to the
trouble of specifying a 'label', surely they would wish to use it
everywhere. I eventually found this excellent tip from Massimo.

Controller:
headers=dict([(c,db[c.split('.')[0]][c.split('.')[1]].label) for c in
rows.colnames])

View:
{{=SQLTABLE(rows, headers=headers)}}

Please could we add this functionality to the core? Or, at least
include it in the book, (sorry if I missed it).

Thanks.

mdipierro

unread,
Sep 10, 2009, 1:28:38 PM9/10/09
to web2py-users
This comes up once in a while and the answer is no.

The problem is that SQLTABLE may need to display the result of a join
where the same field appears has two columns and some columns may be
computed from expressions. They do not have a label and even if they
do, using label would be confusing. This would require so much
configuration that the best solution is NOT to use SQLTABLE but just
loop over the rows.

Massimo

villas

unread,
Sep 10, 2009, 2:07:27 PM9/10/09
to web2py-users
I see it may not be straightforward, but if I may still make a non-
urgent suggestion for future consideration:

1. Check whether it's a normal looking, unambiguous fieldname.
2. If true, and a label exists, then use the label.

After all, if there are any problems with column headers, we could
still set them manually as at present. I'm just trying to make things
easier for the most common case, a standard field column which may
have a label.

Best wishes, David

mdipierro

unread,
Sep 10, 2009, 2:12:12 PM9/10/09
to web2py-users
You can just do

{{=SQLTABLE(rows, headers=dict([('mytable.'+f,db.mytable[f].label) for
f in db.mytable.fields]))}}

villas

unread,
Sep 10, 2009, 6:22:50 PM9/10/09
to web2py-users
Wow, thanks! Don't forget to include in the book.

BTW have you seen the Cakephp book? It's a great collaborative
effort. It would be great if everyone could proposed edits, fix typos
etc and take some of the strain off the core development team. Just a
thought.

Thanks again for the labels! D

yamandu

unread,
Sep 25, 2009, 2:02:13 PM9/25/09
to web2py-users
Maybe one solution could be like is made in other frameworks.

To have 2 objects: one for tables and one for queries, derivating
whatever is possible.
I remember in Delphi things are like that.
Reply all
Reply to author
Forward
0 new messages