[web2py] dbio=False proper way to update every fields not only the one that had changed

91 views
Skip to first unread message

Richard

unread,
Mar 4, 2015, 4:35:37 PM3/4/15
to web...@googlegroups.com
Hello,

I need to do control record update in order to clear some fields values in case a flag is set to false. But doing the update like this :


db(db[request.args(0)].id == request.args(1)).update(**{f: v for f, v in form.vars.iteritems()})

prevent a compute field to be trigger and updating properly as well...

So, to make sure my compute field get it I am doing another update() like this


db
(db[request.args(0)].id == request.args(1)).update()


Which work in one case by not in the other where I actually clear some values like this :


db
(db[request.args(0)].id == request.args(1)).update(f1=None, f2=None, **{f: v for f, v in form.vars.iteritems() if f not in ('f1', 'f2')})


Maybe I should do a db.commit() before my second "blank" update() though it seems to me that it should be a better way and I am concern in reducing db hit.

Thanks

Richard

Anthony

unread,
Mar 4, 2015, 4:44:23 PM3/4/15
to web...@googlegroups.com
Does form.vars include all the variables needed by the compute function?

Also, instead of:


**{f: v for f, v in form.vars.iteritems()}

you can do:

**db[request.args(0)]._filter_fields(form.vars)

_filter_fields will filter out the "id" field as well as any form.vars that are not fields in the table.

Anthony

Richard Vézina

unread,
Mar 4, 2015, 4:55:01 PM3/4/15
to web2py-users
Thanks for answering Anthony,

Actually f1 and f2 belongs to the table, I am filtering them because I want to treat them separately... 

About your question, I am investigating why my compute didn't work... My first impression is that it is not include in form.vars so it not get hit at all... But I am not sure of that...

Richard

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anthony

unread,
Mar 4, 2015, 5:05:56 PM3/4/15
to web...@googlegroups.com
The update should calculate the computed field as long as all the values needed for the calculation are included with the update.

Anthony
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscribe@googlegroups.com.

Richard Vézina

unread,
Mar 5, 2015, 10:17:26 AM3/5/15
to web2py-users
Futher check make me confirm which I thought : 

**{f: v for f, v in form.vars.iteritems()}

Prevent my compute to work since it return only the fields modify by the user... I need to understand how web2py really update on form submit and why compute works in this case...

Or how can I make sure that every fields get update even if there is not any values change which should be what web2py do...

Richard

To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.

Niphlod

unread,
Mar 5, 2015, 3:17:59 PM3/5/15
to web...@googlegroups.com
web2py doesn't know if a field has been edited or not. form.vars (for a standard web2py form) holds EVERY value, irregardless of the ones touched or not by the user...
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Richard Vézina

unread,
Mar 5, 2015, 4:18:06 PM3/5/15
to web2py-users
Yes I struggle with it to make compute works...

I come up with something like this :

after .accepted

for f in db[request.args(0)].fields:
            if f != db[request.args(0)]._id and f != db[request.args(0)].record_review_status:
                form.vars[f] = form.vars.get(f, db[request.args(0)](request.args(1))[f])

record_review_status being my compute field, so I make sure form.vars contain all the missing values which for differents reason may lack from SQLFORM and user input... But still not working, so I am confuse...

Richard

Anthony

unread,
Mar 5, 2015, 5:14:57 PM3/5/15
to web...@googlegroups.com
On Thursday, March 5, 2015 at 4:18:06 PM UTC-5, Richard wrote:
Yes I struggle with it to make compute works...

I come up with something like this :

after .accepted

for f in db[request.args(0)].fields:
            if f != db[request.args(0)]._id and f != db[request.args(0)].record_review_status:
                form.vars[f] = form.vars.get(f, db[request.args(0)](request.args(1))[f])

If it is an update form, all the fields should be in the form already (unless they were explicitly excluded from the form). Also, the above does a separate database select for every missing field -- it would be better to fetch the record just once and then read the fetched record.

Anthony

Richard Vézina

unread,
Mar 6, 2015, 8:58:23 AM3/6/15
to web2py-users
Yah right!


        row = db[request.args(0)](request.args(1))
        for f in db[request.args(0)].fields:
            if f != db[request.args(0)]._id and f != db[request.args(0)].record_review_status:
                form.vars[f] = form.vars.get(f, row[f])


My table is kind of not normalized... Many fields are just showed (not writeable) and the user check a box to review them... These records are missing, but they are not all required for the compute to work.

Should my compute have to be in the form.vars to compute?

Should I explicitly have to redifined it or append it like so :

form.vars[mycompute] = db.table.mycompute.compute

?

Richard

--

Anthony

unread,
Mar 6, 2015, 10:19:35 AM3/6/15
to web...@googlegroups.com
No, the compute field should not be in the form/form.vars. Hard to say what the problem is. Maybe create a minimal app that reproduces the problem.

Anthony
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscribe@googlegroups.com.

Niphlod

unread,
Mar 6, 2015, 3:11:06 PM3/6/15
to web...@googlegroups.com
IMHO the "hiccup" comes from the fact that fields that are writable = False are not rendered as an input/select/whatever, but they're just a TD cell filled with data. Of course the form isn't submitting it.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.

Richard Vézina

unread,
Mar 17, 2015, 11:57:59 AM3/17/15
to web2py-users
Finally, I figured out!!

My compute need the record id, so I shouldn't do this :

if f != db[request.args(0)]._id 

To filter out the record id from the update... I have been miss leading by some example, but I can't recall where they were from, I just search bit and can't retrieve any (neither from book and from sqlhtml.py)...

Anyway, it works now...

:)

Richard
Reply all
Reply to author
Forward
0 new messages