On Wednesday, October 28, 2015 at 10:30:13 AM UTC-4, Carlos Cesar Caballero 
wrote:
>
> I can't do this:
>
> db.business.test = Field.Method(lambda row: 4)db.business.testa = 
> Field.Virtual(lambda row: row.business.test())
> print db(db.business.id > 0).select().first().testa
>
>
> It's a bug?, a feature? or I am having a bad day?
>
> the exception is the clasic "<type 'exceptions.AttributeError'> 'Row' 
> object has no attribute 'testa'"
>
> The same problem happened with two method fields or two virtual fields and 
> I don't want to write the same code twice...
>

I observe the same problem, but I cannot reproduce it with two virtual 
field or two method fields (or with a method field using a virtual field) 
-- only in the exact case you have shown above.

Please submit a Github issue. In the meantime, to avoid redundancy, you can 
do something like:

def test(row):
    value = [some code]
    return value

db.business.test = Field.Method('test', test)
db.business.testa = Field.Virtual('testa', lambda row: test(row))

Of course, in the above example, there is no reason to wrap test() in a 
lambda in testa, but I assume the real lambda will include some additional 
code along with the call to test().

Also, note that it is generally best to include a field name when creating 
a Field.Method or Field.Virtual object, as otherwise the "name" attribute 
of the virtual field will end up being "unknown", which will cause errors 
in some contexts, such as SQLFORM.grid.
 

>
> An extra question: 
>
why I need to do something like:
>
> lambda row: row.tablename.field
>
> something like this looks more elegant:
>
> lambda this: this.field
>
> just saying...
>

You can do:

lambda this: this.tablename.field

I'm not sure why we require the tablename -- maybe for consistency with the 
old style virtual fields, which can be applied to joins and therefore need 
to include the tablename as well as the field name.

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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to