florianletsch
florianletsch

Reputation: 465

Modify column output for sqlform.grid() in Web2py

I have started using web2py for a web application and try to use SQLFORM.grid(...) to display a paginated listing of one of my db-table's data like in the following minimal example.

grid=SQLFORM.grid(query,
    links=links,
    fields=[db.example.date,db.example.foo, db.example.bar])

The db.example.date field contains a Python datetime.datetime object in UTC. At the moment it is displayed just plainly like that. However, I want to have more control about the actual output in a way that I can set the local timezone and modify the output string to have something like "2 hours ago".

As seen in another question[0] I can use the links to insert new columns. Unfortunately I can't seem to sort the rows by a field I have inserted in such way. Also, they are inserted on the right instead of actually replacing my first column. So that does not seem to be a solution.

To sum it up: How do I gain control about the way db.example.date is printed out in the end?

[0] Calculated Fields in web2py sqlgrid

Upvotes: 2

Views: 4009

Answers (3)

Eddie
Eddie

Reputation: 160

If you don't want to have the date always represented in this way as per David Nehme's answer. Just before your grid creation, you can set the db.table.field.represent in the controller.

db.example.date.represent = lambda value, row: value.strftime("%B %d, %Y")

followed by.

grid = SQLFORM.grid(query,....

I use this often when I join tables. If there is a row.field in the represent from the model file it breaks because it then must be more specific, row.table.field.

Upvotes: 0

You need to use prettydate to change the datetime arid format in a humanized string, and call it in the represent parameter of your Field() descriptor. For example :

from gluon.tools import prettydate
db.example.date.represent = lambda v,r: prettydate(r.date)

That way, any display of the db.example.date would be displayed humanized, including through SQLFORM.grid

Upvotes: 0

David Nehme
David Nehme

Reputation: 21597

You can achieve your goal when you define the table in your model. The represent parameter in the Field constructor that you used in define_table will be recognized by the SQLFORM.grid. For example, if you wanted to just print the date with the month name you could put the following in your model.

Field('a_date', type='date', represent=lambda x, row: x.strftime("%B %d, %Y")),

your function could also convert to local time.

Upvotes: 4

Related Questions