On Sunday, December 9, 2018 at 10:39:42 AM UTC-8, Anthony wrote:
>
> 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'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.

 

> 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
>

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?

For this use case, are hooks any more efficient than a second query?

/dps


> 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.

Reply via email to