Compute insert-only

118 views
Skip to first unread message

Louis Amon

unread,
Mar 10, 2015, 11:21:26 AM3/10/15
to web...@googlegroups.com
I'm trying to hard-code URLs for SEO purposes so I have a function ("make_url(row)") that builds the paths based on other fields.

I was thinking of using "compute=make_url", but it seems that update operations also call the compute.


Is there a way to use compute only on INSERT operations, and not on UPDATE ?

Niphlod

unread,
Mar 10, 2015, 12:10:12 PM3/10/15
to web...@googlegroups.com
code an _after_insert callback.

Louis Amon

unread,
Mar 10, 2015, 12:14:54 PM3/10/15
to web...@googlegroups.com
That’d do the trick nicely.

Thanks Niphlod!


--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/I1acweQhAEY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anthony

unread,
Mar 10, 2015, 1:00:08 PM3/10/15
to web...@googlegroups.com
Might be easier with a _before_insert callback, so you can add the new value along with the initial insert instead of after:

db.mytable._before_insert.append(lambda fields: fields.update(url=make_url(fields)))

Anthony

Louis Amon

unread,
Mar 10, 2015, 1:09:12 PM3/10/15
to web...@googlegroups.com
My url builder requires the « id » field so _before_insert wouldn’t work.

That’s a good suggestion though!

Thanks Anthony :)

Anthony

unread,
Mar 10, 2015, 1:41:55 PM3/10/15
to web...@googlegroups.com
Got it. In that case, compute wouldn't have worked anyway.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.

黄祥

unread,
Nov 5, 2015, 6:13:46 AM11/5/15
to web2py-users
compute field not work when using after insert callback too.
e.g. 
def __after_insert_delivery_order(f, id):
db(db.customer.id == f.customer).update(last_delivery_order_net = f.gross - f.tarra) # work
#db(db.customer.id == f.customer).update(last_delivery_order_net = f.net) # not work

def on_define_delivery_order(table): 
table._after_insert.append(__after_insert_delivery_order)
table.net.compute = lambda r: r['gross'] - r['tarra']

db.define_table('delivery_order', 
Field('delivery_order_no'), 
Field('customer', 'reference customer'), 
Field('tarra', 'integer'),
Field('gross', 'integer'),
Field('net', 'integer'),
on_define = on_define_delivery_order, 
format = '%(delivery_order_no)s')

not sure is it bug, or normal behaviour.

best regards,
stifan

Massimo Di Pierro

unread,
Nov 11, 2015, 9:59:12 AM11/11/15
to web2py-users
I see the problem, the computed field is computed by the insert function while f in the _after_insert_delivery_order are the record parameters as passed to the insert function (before the field is computed). So I'd say this is not supposed to work. Open a ticket with your code and we will consider the improvement.

黄祥

unread,
Nov 11, 2015, 4:29:50 PM11/11/15
to web2py-users
done

thanks and best regards,
stifan
Reply all
Reply to author
Forward
0 new messages