You are right. It is not documented, but with _after_insert, the
f.fieldname notation does work with a regular insert but not with
bulk_insert. It also does not work with _before_insert. I'm not sure why
the inconsistency, but it is probably best to stick with the documented
method using f['fieldname'].
Anthony
On Thursday, April 21, 2016 at 7:47:30 PM UTC-4, 黄祥 wrote:
>
> yes, you all right, in the past i learned the after_insert callback can be
> access via f.amount, yet it still work when using insert, but not
> bulk_insert (must use dict f['amount'] ), please correct me if i'm wrong
> about my conclusion
>
> thanks and best regards,
> stifan
>
> On Thursday, April 21, 2016 at 9:03:16 PM UTC+7, Anthony wrote:
>>
>> On Thursday, April 21, 2016 at 1:57:20 AM UTC-4, 黄祥 wrote:
>>>
>>> meet strange behaviour in after_insert callback, when using variable
>>> (e.g. f.amount) is not work, but when using dict (e.g. f['amount'] ) work
>>> models/db_wizard_4_subscription.py
>>> # not work
>>> def __after_insert_subscription_detail(f, id):
>>> unit = float(f.amount) / float(f.nav_per_unit)
>>>
>>
>> Why would you expect that to work? As noted in the documentation:
>>
>> Here f is a dict of fields passed to insert or update...
>>
>>
>> Anthony
>>
>>
>>
>>>
>>> """
>>> # work
>>> def __after_insert_subscription_detail(f, id):
>>> unit = float(f['amount'] ) / float(f['nav_per_unit'] )
>>> """
>>>
>>> def on_define_subscription_detail(table):
>>> table._after_insert.append(__after_insert_subscription_detail)
>>>
>>> db.define_table('subscription_detail',
>>> Field('subscription_no', 'reference subscription_header'),
>>> Field('account', 'reference account'),
>>> Field('nav_date', 'date'),
>>> Field('nav_per_unit', 'double'),
>>> Field('amount', 'double'),
>>> Field('fee', 'double'),
>>> Field('fee_amount', 'double'),
>>> Field('unit', 'double'),
>>> auth.signature,
>>> on_define = on_define_subscription_detail,
>>> format = '%(subscription_no)s')
>>>
>>> Traceback (most recent call last):
>>> File "/Users/MacBookPro/site/web2py/gluon/restricted.py", line 227, in
>>> restricted
>>> exec ccode in environment
>>> File
>>> "/Users/MacBookPro/site/web2py/applications/mutualfunds/controllers/install.py",
>>>
>>> line 221, in <module>
>>> File "/Users/MacBookPro/site/web2py/gluon/globals.py", line 417, in
>>> <lambda>
>>> self._caller = lambda f: f()
>>> File
>>> "/Users/MacBookPro/site/web2py/applications/mutualfunds/controllers/install.py",
>>>
>>> line 201, in index
>>> 'fee_amount' : 0}, ] )
>>> File
>>> "/Users/MacBookPro/site/web2py/gluon/packages/dal/pydal/objects.py", line
>>> 831, in bulk_insert
>>> ret and [[f(item, ret[k]) for k, item in enumerate(items)] for f in
>>> self._after_insert]
>>> File
>>> "/Users/MacBookPro/site/web2py/applications/mutualfunds/models/db_wizard_4_subscription.py",
>>>
>>> line 43, in __after_insert_subscription_detail
>>> unit = float(f.amount) / float(f.nav_per_unit)
>>> AttributeError: 'dict' object has no attribute 'amount'
>>>
>>> controllers/install.py
>>> db.subscription_detail.bulk_insert([{'subscription_no' : 1, 'account' :
>>> 1, 'nav_date' : '2016-01-01',
>>> 'nav_per_unit' : 1.0, 'amount' : 1, 'fee' : 0,
>>> 'fee_amount' : 0},
>>> {'subscription_no' : 2, 'account' : 1, 'nav_date' : '2016-01-02',
>>> 'nav_per_unit' : 1.1, 'amount' : 2, 'fee' : 0,
>>> 'fee_amount' : 0}, ] )
>>>
>>> anyone know about this behaviour? is it normal?
>>>
>>> thanks and best regards,
>>> stifan
>>>
>>
--
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.