On Wednesday, December 12, 2018 at 5:38:29 AM UTC-8, Anthony wrote:
>
> I'm actually taking 2 values from the request as the query (q = mytable.x 
>> == request.vars.X and mytable.y == request.vars.Y)
>> [X is  actually not from the vars, but from the env's].  
>> update_or_insert() takes care of making these more or less unique.  The 
>> downside of that is only knowing the id if the insert takes place, so I can 
>> tell if it was an update but not where.  Doing a second query is possible, 
>> I admit.
>>
>
> If you need to do an update-or-insert but need the ID in case of update, 
> then do not use .update_or_insert -- just write your own logic, as it is 
> quite simple.
>  
>

Ah, well, your answer below is way cooler and then I don't need the ID in 
the calling procedure.
 

> a _before_update hook  gives me the id, but how do I add the num_updates 
>> field?  lambda s, f: f.append(dict('num_updates', 2)) doesn't work 
>> (gives an AttributeError).  The ticket says f is really an OpRow, and 
>> anyway I don't want to stick a dict *into* it.  Python dict's have an 
>> update method to, um, concatenate a dict to a dict; does OpRow have 
>> something like that?  And how can I get the present value?
>>
>
> The second argument passed to _before_update is a Row-like object of the 
> fieldnames/values of the update -- so just do this:
>
> def mytable_before_update(s, f):
>   # f['num_updates'] also works.
>   f.num_updates = db.mytable.num_updates + 1
>
> With a lambda, you can do:
>
> lambda s, f: f.set_value('num_updates', db.mytable.num_updates + 1)
>
> Though the .set_value method is not part of the public API, so may not 
> remain backward compatible.
>
> Note, you do not need the current value of num_updates -- if you use an 
> expression like above, the database will do the addition for you. To see 
> what the generated query looks like, try printing db(db.mytable.id == 
> some_id)._update(num_updates=db.mytable.num_updates + 1).
>
> Anthony
>

Very nice! Thanks for all the help; I am always impressed by the expertise 
here.

/dps
 

-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to