Syntax question: db(db.mytable.id == request.post_vars["id"]).update(**{key:value})

46 views
Skip to first unread message

Henk huisman

unread,
Dec 6, 2016, 5:44:56 PM12/6/16
to web2py-users
Is this correct syntax?

if request.post_vars["id"]:
       
for key, value in request.post_vars.items():
      db
(db.mytable.id == request.post_vars["id"]).update(**{key:value})


Niphlod

unread,
Dec 7, 2016, 4:06:07 PM12/7/16
to web2py-users
I'd loop collecting the needed updates in a dict and then issue a single update at the end with the **dict

Henk huisman

unread,
Dec 8, 2016, 9:27:22 PM12/8/16
to web2py-users
Thanks,

I wrote this code and seems to work well...from an ajax call.
Post is with or without id; depending on if its a new record or an update

def save_selected():
    a={} 
    if request.vars["id"]:
        for key, value in request.vars.items():
            if key!="id":
                a[key]=value
        db(db.mytable.id == request.vars["id"]).update(**a)
    else:
        for key, value in request.vars.items():
            a[key]=value
        db.mytable.insert(**a)
    return 

Reply all
Reply to author
Forward
0 new messages