Thanks Anthony 

I would like to get the later suggestion working but not sure how to pass 
the row object from a field or if this is what you mean the signature it 
needs?


My function is updated to this...   # There were typos in the filed name 
which weren't helping 
(e.g. person.type should have been person.person_type - I know it's not 
good practice but was trying to avoid restricted words like 'type') 

def get_user_short_name(row):
    display_name = "NAME ERROR - unknown person type"

    if row.person.person_type == 'CLIENT':
        if row.person.referrer_ref:
            display_name = '%s %s' % (row.person.referrer_ref, 
row.person.first_name)
        else:
            display_name = 'PAP/%s %s' % (row.person.id, row.person.first_name)

    elif row.person.person_type == 'CONTACT':
        display_name = '%s, %s' % (row.person.last_name, row.person.first_name)
    print 'get_user_short_name(): returning name: >%s< for id >%s<' % 
(display_name, row.person.id)
    return display_name




and have tried various formats for the Virtual field definition...

db.person.display_name = Field.Virtual('display_name', get_user_short_name)
db.person.display_name = Field.Virtual('display_name', get_user_short_name())
db.person.display_name = Field.Virtual('display_name', get_user_short_name(row))

 
but none are working for me.






On Monday, 29 May 2017 03:01:49 UTC+1, Anthony wrote:
>
>
> When I add code to display the contents    'Shortname: %s' % 
>> person.short_name 
>> (even after I have edited the table - which I understand should trigger 
>> the shortname update)
>>
>
> Updating the record will only trigger the compute field update if *all* 
> the fields needed by the function are included in the updated fields. The 
> compute function will not fetch non-updated fields from the database in 
> order to calculate the computed value.
>  
>
>> and for the seond method    'DisplayName: %s' % person.display_name
>>
>> I get an attribute error...
>>
>> Traceback (most recent call last):
>>   File "/home/peter/web2py/gluon/restricted.py", line 227, in restricted
>>     exec ccode in environment
>>   File 
>> "/home/peter/web2py/applications/PAPILLON_AIM/controllers/default.py", line 
>> 2256, in <module>
>>   File "/home/peter/web2py/gluon/globals.py", line 417, in <lambda>
>>     self._caller = lambda f: f()
>>   File "/home/peter/web2py/gluon/tools.py", line 4241, in f
>>     return action(*a, **b)
>>   File 
>> "/home/peter/web2py/applications/PAPILLON_AIM/controllers/default.py", line 
>> 305, in view_person
>>     'DisplayName: %s' % person.display_name
>>   File "/home/peter/web2py/gluon/packages/dal/pydal/objects.py", line 90, 
>> in __getattr__
>>     raise AttributeError
>> AttributeError
>>
>
> When defining a virtual field, inside the function, you must use 
> row.tablename.fieldname to refer to each field, not just row.fieldname.
>
> As an aside, why not change the get_user_short_name function to take a 
> single row object rather than each of the individual field values? That 
> way, instead of having to do:
>
> Field.Virtual('display_name', lambda row: get_user_short_name(...))
>
> you can simply do:
>
> Field.Virtual('display_name', get_user_short_name)
>
> In other words, as long as you are creating a function solely for this 
> purpose, just give it the signature it needs without having to wrap it in a 
> lambda.
>
> Anthony
>

-- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to