By the way, I don't know how about everyone else, but I always confuse compute and virtualfield feature, and each time I need to check document to know which one is for reading from db and the other is for writing into db. How about giving an alias "onvalidation" to the feature "compute", so that people can easily know how and when it works? For example:
>>> db.define_table('a', ..., Field('z', onvalidation=lambda row: row['x']*3)) On Jul 23, 2:01pm, mdipierro <mdipie...@cs.depaul.edu> wrote: > +1 > > On Jul 22, 7:08 pm, "mr.freeze" <nat...@freezable.com> wrote: > > > > > Assuming you meant: > > r.y.y.y.t > > It works because db.a.y is a self referencing field. Inserting into > > db.a doesn't return an int, but a Reference, which has a __getattr__ > > that returns the record it references. Any number of y's should work: > > r.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.t > > 't' is the virtual field which returns the length of the computed > > field, which in turn is 3*x. 'x' in this case is 'hello', so t is 15. > > You could also do: > > r.y.y.y.y.z > > to get the computed field which should return: > > hellohellohello > > > On Jul 22, 6:50 pm, "mr.freeze" <nat...@freezable.com> wrote: > > > > It shouldn't. It should throw an error at the print statement because > > > 'a' is not defined. > > > > On Jul 22, 6:33 pm, mdipierro <mdipie...@cs.depaul.edu> wrote: > > > > > The following code uses: > > > > 1) a computed field > > > > 2) a self reference > > > > 3) a virtual field > > > > > >>> db.define_table('a',Field('x'),Field('y','reference > > > > >>> a'),Field('z',compute=lambda row: row['x']*3)) > > > > >>> class A: > > > > >>> def t(self): return len(self.a.z) > > > > >>> db.a.virtualfields.append(A()) > > > > >>> r=db.a.insert(x='hello') > > > > >>> r.update_record(x=r.x,y=r) > > > > >>> print a.y.y.y.y.t > > > > > 15 > > > > > Can you explain how it works?