Should be:
Field.Virtual('age', lambda row: diff_in_years(row.personal_data.dob))
Virtual field functions must reference the table name as well as the field
name.
Anthony
On Tuesday, February 18, 2014 5:28:26 PM UTC-5, Leandro E. Colombo Viña
wrote:
>
> Hi Guys!
>
> I've got a little problem trying to use Virtual Fields. I can't make them
> work. I follow the example in the book and nothing, I don't have a clue of
> what I'm doing wrong...
> The idea is to have a virtual field that calculates the age of the person
> based on his day of birth...
>
> Here is the model:
> db.define_table('personal_data',
> ....., # another fields
> Field('dob', 'date', requires=IS_DATE()),
> ....., # more fields
> auth.signature
> )
> db.personal_data.age = Field.Virtual('age', lambda row:
> diff_in_years(row.dob))
>
> Where the auxiliary function is:
> from dateutil import relativedelta as rdelta
> def diff_in_years(date):
> try:
> return rdelta.relativedelta(datetime.datetime.today().date(),
> date).years
> except:
> return None
>
> And here is my test controller:
> def prueba():
> response.flash = db.personal_data(uid=auth.user.id).age
>
> The traceback says that "Row" doesn't have the attribute "age".
>
> I've also tried with a method (Field.Method), with no luck. I always have
> the same error....
>
> Any help would be appreciated!
> LEO
>
--
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/groups/opt_out.