Hi all,

I spent hours yesterday trying to implement virtual fields correctly
following the book, and based on that I found some behaviour which
could be better explained.

Reference: http://web2py.com/book/default/chapter/06#Virtual-Fields
web2py version: 1.88.2

1. Selecting a virtual field like a normal field in select() part of
the query will throw a KeyError. However, the virtual field is still
getting selected.

2. This part:

class MyVirtualFields(object):
...
db.mytable.virtualfields.append(MyVirtualFields())

should come before the db.select for the virtual fields to be
selected. The model is a good place for this part of the code. This is
in slight contrast to the second code example where
rows.setvirtualfields(...) is called after the rows are selected. The
difference is slightly confusing.

3. Using the lazy loading techniques makes it impossible to select the
virtual fields directly in a query. You will need to iterate through
the selected rows and explicitly extract the
row.mytable.myvirtualfield() as shown in example. The need to loop
through the rows again (against using the rows directly in say, an
SQLTable) may not really be desirable in a web2py app situation, and
should not be considered the general use case.

Please correct me if anything here is wrong. The point is that I spent
a lot of time and trial-error methods to figure this out, which we can
avoid for other people if we explain this part better in the book.

Reply via email to