How do you change xml format of datetime?

77 views
Skip to first unread message

nick name

unread,
Feb 25, 2011, 7:57:54 PM2/25/11
to web2py-users
I am using the xml serializer to print datetime values. I need to put
it out in .isoformat() e.g. 2011-12-31T10:20:30 (possibly with a
second fraction, but not necessarily). However, I've been unable to
find how to do that:

db.field.represent = lambda x: x.isoformat() doesn't make a difference
(according to my readling of sqlhtml.py, dal.py it should)

neither does the format in an IS_DATETIME(format=...) validator

How do I override the xml representation of a field?

Massimo Di Pierro

unread,
Feb 25, 2011, 10:37:08 PM2/25/11
to web2py-users
Just to make sure I understand can you post an example of the code you
are using?

nick name

unread,
Mar 25, 2011, 7:12:18 PM3/25/11
to web...@googlegroups.com, Massimo Di Pierro
(sorry for taking so long to reply, I've been sidetracked by other things.....)

While trying to prepare a concise example and trace through everything, I was able to figure out the problem - I was using as_list(). Turns out that as_list() has a parameter "datetime_as_str" which defaults to True - as a result, my xml was converted to string upon calling as_list() (as soon as I got it from the database), and no converter was used.

I think it's a good idea to add it to the "as_list()" and "as_dict()" documentation in Chapters 6 and 9 of the book where it is mentioned. As far as I Can tell, the only location it is documented is in the epydoc/source http://www.web2py.com/examples/static/epydoc/web2py.gluon.dal.Rows-class.html#as_list

(Note also that the :param definition is not being properly parsed by epydoc)

For me it was very confusing and unexpected - when otherwise py2web is a very "principle of least surprise" kind of framework. I would not have expected this formatting to happen at all, and if it does happen, to be done using the represent=... field.

Thanks again for web2py, it is awesome!
Reply all
Reply to author
Forward
0 new messages