Hi all, 

I don't get the following errors and results. My goal is to have a virtual 
field and i can't get it to work the way i want it - and what i think is 
described in the book as it should work. What am i doing wrong here? 

Below are serveral independent samples of what i thought had to work, 
though old_style_virtual_field_test2 will never work and is obvious. 

Thanks for helping out. 
Remco Boerma

# web2py 2.9.5-stable+timestamp.2014.03.16.02.35.39

def new_style_virtual_field_test():
    db = DAL('sqlite:memory',pool_size=1)
    db.define_table('myorder',
        Field('a','integer'),
        Field('b','integer')
    )
    db.myorder.c = Field.Virtual(lambda row:row.a * row.b)
    db.myorder.insert(a=2,b=3)
    return db().select(db.myorder.ALL).first().as_json()
    # {"a": 2, "id": 1, "b": 3}
    # where is c?
    
    return db().select(db.myorder.a, db.myorder.b, 
db.myorder.c).first().as_json()
    # <class 'sqlite3.OperationalError'> no such column: None.unknown
    # (self=<gluon.dal.SQLiteAdapter object>, *a=('SELECT myorder.a, 
myorder.b, None.unknown FROM myorder;',), **b={})
    
    # where has my C field gone to? 
    
    
def old_style_virtual_field_test1():
    db = DAL('sqlite:memory',pool_size=1)
    db.define_table('myorder',
        Field('a','integer'),
        Field('b','integer')
    )
    class MyVirtualFields(object):
        def c(self):
            return self.myorder.a * self.myorder.b
    db.myorder.virtualfields.append(MyVirtualFields())
    db.myorder.insert(a=2,b=3)
    # return db().select(db.myorder.ALL).first().as_json()
    # {"a": 2, "id": 1, "b": 3}
    # where is c? 
    
    return db().select(db.myorder.a, db.myorder.b, 
db.myorder.c).first().as_json()
    # AttributeError: 'Table' object has no attribute 'c'

    # it doesn't ? I thought i declared to be virutally present

def old_style_virtual_field_test2():
    db = DAL('sqlite:memory',pool_size=1)
    db.define_table('myorder',
        Field('a','integer'),
        Field('b','integer')
    )
    class MyVirtualFields(object):
        def c(self):
            return self.myorder.a * self.myorder.b
    db.myorder.setvirtualfields(myorder=MyVirtualFields())
    # <type 'exceptions.AttributeError'> 'Table' object has no attribute 
'setvirtualfields'
    # maybe a bit more clarity in the documentation would be nice. I 
understand the purpose
    # but it's easily overlooked. 

def old_style_virtual_field_test3():
    db = DAL('sqlite:memory',pool_size=1)
    db.define_table('myorder',
        Field('a','integer'),
        Field('b','integer')
    )
    class MyVirtualFields(object):
        def c(self):
            return self.myorder.a * self.myorder.b
    db.myorder.insert(a=2,b=3)

    rows = db(db.myorder).select()
    # now i have to apply them after selecting? That's counter intuitive
    # i thought it would be in the model, not the controller 
    rows.setvirtualfields(myorder=MyVirtualFields())
    # also note the setvirtualfields has to be applied to the rows, not to 
a single row. 

    return rows.first().as_json();
    # {"a": 2, "c": 6, "b": 3, "id": 1} 
    # yeey!!



-- 
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