In the Official web2py Book I read about DAL's Virtual Fields that:

"...each method of the class that takes a single argument (self) is a new 
virtual field."

It seems however that any function of the class is treated as a new field 
and called to calculate its value. As an example I added a support function 
to the class:

class MyVirtualFields(object):
    def total_price(self):
        return self.support_function(self.item.unit_price, 
self.item.quantity)
    def support_function(self, price, quantity):
        return price*quantity

And on row retrieval I receive the following exception:
TypeError: support_function() takes exactly 3 arguments (1 given)

As a workaround I can add default values for arguments and additional 
checks in the support function. But I wonder what is the proper way to add 
a non-field function to the class?

Thank you!

Dmitry
from gluon.dal import DAL, Field
db = DAL('sqlite://sqlite.db')
db.define_table('item',
    Field('unit_price','double'),
    Field('quantity','integer'))

class MyVirtualFields(object):
    def total_price(self):
        return self.support_function(self.item.unit_price, self.item.quantity)
    def support_function(self, price, quantity):
        return price*quantity

db.item.virtualfields.append(MyVirtualFields())
db.item.insert(unit_price=10, quantity=50)
rows = db(db.item).select()

Reply via email to