decremental subtraction of values in DB field 2

27 views
Skip to first unread message

mostwanted

unread,
Sep 17, 2019, 5:25:07 AM9/17/19
to web2py-users
For a while now i have been trying to simulate excel functionality without success, i have had some assistance from Anthony but i still failed to make it work but its all because of my limited imagination, i just didn't understand his solution.


I am working on an application where I am changing a spread sheet document into a web application, the problem i am facing with one of the calculations is that I have to calculate for the answer_x and to get the value of answer_x i have to subtract the previously entered reading_x value from the newly entered reading_x value e,g in excel the value of answer_x is (=reading_x2 - reading_x1 ) or E1 would be (=D2-D1), i hope this is clear.

This was my solution:

db.define_table('subtract_value',
               
Field('reading_x', 'integer', requires=IS_NOT_EMPTY()),
               
Field('answer_x', compute=lambda r:r['reading_x']-r['reading_x']))

But Anthony suggested i do this:

def subtract_value_before_update(*args, **kwargs):
    db
.subtract_value.answer_x.compute = lambda r: (db.subtract_value.reading_x* -1) + r.reading_x

db
.define_table('subtract_value',
   
Field('reading_x', 'integer'),
   
Field('answer_x', 'integer', default=0))

db
.subtract_value._before_update.append(subtract_value_before_update)

I believe this solution can work if i only understood it but i dont, i am missing something because this code is not doing any calculations at all, I am not getting the calculated values I want but it could be the way i wrote it, i could be missing something or not doing something right!

All i want i want to do is to calculate for answer_x by subtracting the previously entered value of reading_x from the newly entered value of reading_x.


How can i achive my task?


Regards;


Mostwanted

villas

unread,
Sep 19, 2019, 11:06:50 AM9/19/19
to web2py-users
Just a suggestion, but why not try something easy, like:
  • add a date field to your model
  • when saving a record from an entry form, use onvalidation
  • in the onvalidation function, find the previously dated record
  • compare the previous record with the current entries
  • calculate the meter reading 'delta' value and fuel statistics etc and update these fields in your current record
OR
  • Do not save any delta or fuel stats at the time of saving a meter reading record.
  • Simply select the records you require to analyse and then calculate all the extra info with python at the time you need to report the data,

Both of the above are within your capabilities. 

As I said, this is simply a suggestion, but it save time and complexity :)
Best wishes for your project
Reply all
Reply to author
Forward
0 new messages