Here's the relevant code:
def update_or_insert(self, _key=DEFAULT, **values):
if _key is DEFAULT:
record = self(**values)
elif isinstance(_key, dict):
record = self(**_key)
else:
record = self(_key)
if record:
record.update_record(**values)
newid = None
else:
newid = self.insert(**values)
return newid
It uses the .update_record method, so I think the answer to your two
questions is yes. But do you need to use .update_or_insert for other
reasons? If not, can you just do:
db.define_table('mytable', Field('myfield'), Field('num_updates', default=1
))
myid = db.mytable.insert(myfield='some value')
db(db.mytable.id == myid).update(myfield='new value', num_updates=db.mytable
.num_updates + 1)
I suppose you could add a _before_update hook to automatically add the
num_updates field to any update, but you would have to do that if using
.update_or_insert as well.
Anthony
On Tuesday, December 4, 2018 at 11:11:18 PM UTC-5, Dave S wrote:
>
> I'm using update_or_insert
> <URL:
> http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#update_or_insert
> >
> for a table that's keeping track of a certain type of request.
> I'd like to keep track of how many times a particular row has been
> updated, and I'm wondering if I can do that with update_or_insert().
>
> Sub-question 1: if a row has fields that aren't in the argument list to
> update_or_insert() left with their current values?
>
> Sub-question 2: If a row exists, can you reference it's current value in
> an expression for one the arguments?
>
> If there's nothing in update_or_insert(), can I use a hook, er, callback
> to do the counting? Does update_or_insert() use before/after_insert() when
> it does an insert instead of an update, or does it always use
> before/after_update() ?
>
> /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.