You can use compute type field (search for compute in the book) you just
have to put your delta in a lambda function... That if you want to store the
computed value...

Or you can use virtual field that can compute also (not sure about that)
your delta will be execute each time to call you select view. That is what
you search for since the former option will calculate the delta only when
you enter a new record...

It is maybe possible to make a cron job to update the state of you computed
stored delta each day for example... But if you don't need to filter base in
you select request base on the delta the virtual column is maybe the best
option.

Hope it help.

Richard

On Wed, Oct 12, 2011 at 10:49 AM, Nate Atkinson <nja.perso...@gmail.com>wrote:

> Hi,
>
> The company I work for places environmental measuring devices at
> different locations. I've designed a simple database to keep track of
> where we have devices. One thing that's important to us is how long a
> device has been on site.
>
> It's probably easiest to understand if I just paste some code. Here's
> a sample:
>
>
> Controller:
>
> def viewbystatus():
>    places = db(db.plads.status == request.args(0)).select() or
> redirect(URL('index'))
>    statusstring = db.status(request.args(0)).status
>    return dict(places=places, status=statusstring)
>
>
> View:
>
> {{extend 'layout.html'}}
> {{def Linkify(element, id, method): return A(element,
> _href=URL(method, args=(id)))}}
> <h1>{{=status}}</h1>
> <h3>{{=str(len(places)) + ' sites'}}</h3>
> <table>
> <thead><tr>
> <td>Name</td><td>Address</td><td>Zipcode</td><td>City</td><td>State</
> td><td>Sitefinder</td><td>NumBoxes</td><td>DatePlaced</td>
> </tr></thead>
> {{for row in places:}}
> {{elements = [Linkify(element, row.id, 'show') for element in
> [row.name, row.address, row.zipcode, row.city, row.state.state,
> row.sitefinder.wardname, row.numboxes, row.dateplaced]]}}
> {{=TR(elements)}}
> {{pass}}
> </table><br>
> <br>
> [{{=A('Return to index', _href=URL('index'))}}]
>
>
>
> Essentially, I would like to replace the DatePlaced column in the view
> with something like DaysOnSite. It's a simple matter of
> datetime.date.today() - row.dateplaced.
>
> I could easily calculate this in the for loop in the view. Is that
> correct practice? Should I be calculating it in the controller? Should
> I add a column in the database and recalculate/update on each access?
>
> For some context, there are a couple of different views where it would
> be useful to see the number of days on site.
>
> So, what's the correct thing to do here?

Reply via email to