For the time being you can use this syntax: db=DAL() db.define_table('a',Field('b','integer')) for i in range(10): db.a.insert(b=i) def lazy(f): def g(self,f=f): import copy self=copy.copy(self) return lambda *a,**b: f(self,*a,**b) return g class Scale: @lazy def c(self,scale=1): return self.a.b*scale db.a.virtualfields.append(Scale()) for row in db(db.a).select(): print row.b, row.c(1), row.c(2), row.c(3)
On Aug 14, 10:57 am, Santiago Gilabert <santiagogilab...@gmail.com> wrote: > anyone? > > I found that someone else asked about the same issue 5 month ago but there > are no comments about it. > > http://groups.google.com/group/web2py/browse_thread/thread/845e6cdef5... > > Thanks > Santiago > > On Sat, Aug 13, 2011 at 7:07 PM, Santiago <santiagogilab...@gmail.com>wrote: > > > > > > > > > Hello, > > > I have the following definitions in db.py > > > class ElectionVirtualField(object): > > ... > > def is_presidential(self): > > def lazy(self=self): > > return self.election.category == 'P' > > return lazy > > ... > > > db.define_table('election', > > ... > > Field('category', length=1, label=T('Category') > > ... > > > The problem I have is that when I add a bunch of elections in dict() > > (key = election.id, value = election row) and then traverse the dict, > > I get wrong values from is_presidential() > > > Example: > > > elections = dict() > > for election in db(db.election).select(): > > elections[election.id] = election > > > for (k, v) in elections.items(): > > print k , ' ', v.category, ' ', v.is_presidential() > > > Output: > > 81 D True > > 79 P True > > > As you can see, it returns True for both, but for the first one, it > > should return False. > > > If I change the code to reload the election from the database, the > > output is different: > > > Example: > > > elections = dict() > > for election in db(db.election).select(): > > elections[election.id] = election > > > for (k, v) in elections.items(): > > reloaded_election = db.election(k) > > print k , ' ', v.category, ' ', v.is_presidential() > > > Output: > > > 81 D False > > 79 P True > > > Does this mean that we can't save rows from DB on Build in types ? > > > Thanks in advance, > > Santiago