update_or_insert - handling record created/modified timestamps

100 views
Skip to first unread message

Carl Hunter Roach

unread,
Jul 10, 2014, 12:00:23 PM7/10/14
to web...@googlegroups.com
I am using Web2py on an appengine project and each of my tables include two 'datetime' fields cDate and mDate to record when the record was created and modified respectively.

Last time I used Web2py I'd check if  a record existed and call insert() or update_record() appropriately.

Now, I can potentially use update_or_insert() but I don't see how I can pass in cDate and mDate for "insert" but mDate only for "update_record"

Does web2py have create/modify fields like it adds an "id" to all tables and handle this for me?

Massimo Di Pierro

unread,
Jul 12, 2014, 3:44:32 AM7/12/14
to web...@googlegroups.com
Mind web2py offers all of this out of the box:

   db.define_table('stuff',Field('name'), auth.signature)

auth.signature adds created_on, created_by, modified_on, modified_by and is_active. These fields can be used by auth for full auditing if enabled by

   auth.enable_record_versioning(db)

Anyway, if you want to do this at lower level:

   Field('mDate',default=f,update=g)

where f is a value or a a function that returns the value to be used when the record is created. g is a value or a function that returns a value to be used when the record is updated.

I would strongly suggest using auth.signature instead. 

Leonel Câmara

unread,
Jul 15, 2014, 5:32:57 AM7/15/14
to
If you use auth.signature pay attention that it uses request.now as the default for the timestamps which uses the timezone of wherever it is running. In my opinion time should always saved in the DB in UTC for sanity's sake and future proofing. Seriously, request.now is retarded.

Massimo Di Pierro

unread,
Jul 15, 2014, 10:06:33 AM7/15/14
to web...@googlegroups.com
Just put 

    request.now = request.utcnow

before you define your models (including auth).
Reply all
Reply to author
Forward
0 new messages