Model "before save" functionality?

27 views
Skip to first unread message

User

unread,
Sep 25, 2013, 8:34:01 PM9/25/13
to web...@googlegroups.com
I have a model with latitude and longitude fields.  The lat and lng should be computed from the address via Google geocoding.  Whenever the model is inserted or updated I want to update the lat and lng from the address of the model.
 
In Yii PHP framework there a number of events that you can customize surrounding models.  One of them is the beforeSave event (see http://www.yiiframework.com/doc/api/1.1/CActiveRecord#beforeSave-detail)  which is called every time before a model is saved to the database and which I could use to call a geocode function and stuff the resulting lat and lng into fields before saving to the database.  This works no matter where you are saving the model from.
 
How can I get similar behavior in web2py?
 
Currently I have a controller where in the "create()" function I use onvalidation parameter of the form.process method to trigger the geocoding.  This works but then I would need to repeat this logic in an "update()" controller function.  And how would I get this to work when creating or updating records from the appadmin interface?

Massimo Di Pierro

unread,
Sep 25, 2013, 9:01:23 PM9/25/13
to web...@googlegroups.com
Look up
db.table._before_insert
db.table._before_update

There is also

form.process(onvalidation = lambda form: .....)
Reply all
Reply to author
Forward
0 new messages