I just did this as a test and it worked:
>>> db=DAL('sqlite:memory:')
>>> db.define_table('test',Field('quantity','integer'))
>>> class VirtualFields():
... def profit(self): return self.test.quantity*100
>>> db.test.virtualfields.append(VirtualFields())
>>> db.test.insert(quantity=5)
1
>>> rows=db(db.test.id>0).select()
>>> for row in rows: print row
...
<Row {'update_record': <function <lambda> at 0x1b8dbb0>, 'profit':
500, 'id': 1, 'delete_record': <function <lambda> at 0x1b8dab0>,
'quantity': 5}>
Sure you are running an updated version of web2py? Which os? which
version?
On Apr 4, 11:09 pm, Iceberg <[email protected]> wrote:
> Actually I did notice that typo, I tried the append() already, my code
> is:
>
> class VirtualFields:
> def profit(self): return self.Orders.income - self.Orders.cost
> db.Orders.virtualfields.append(VirtualFields())
>
> this line runs fine, but later when I need to access a virtual field,
> an exception is raised:
>
> File "C:\DOWNLOAD\google_appengine\web2py\gluon\sql.py", line 658,
> in __getitem__
> return dict.__getitem__(self, key)
> KeyError: 'profit'
>
> Ideas? Thanks.
>
> -Iceberg
>
> On Apr5, 12:00pm, Thadeus Burgess <[email protected]> wrote:
>
> > I think its
>
> > db.table.virtualfields.append()
>
> > -Thadeus
>
> > On Sun, Apr 4, 2010 at 10:51 PM, Iceberg <[email protected]> wrote:
> > > Hi there,
>
> > > I search virtualfields inhttp://www.web2py.com/bookbutfound
> > > nothing.
>
> > > So I read this post instead:
> > > https://groups.google.com/group/web2py/browse_frm/thread/d59ee3d6c2e6...
>
> > > But I found that
> > > db.table.virtualfields.add(ComputedFields())
> > > doesn't work.
>
> > > Only this work:
> > > rows.setvirtualfields(table=ComputedFields())
>
> > > Does the former syntax really supposed to exist?
>
> > > I am using web2py 1.75.4
>
> > > Regards,
> > > Iceberg
>
> > > --
> > > You received this message because you are subscribed to the Google Groups
> > > "web2py-users" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected].
> > > For more options, visit this group
> > > athttp://groups.google.com/group/web2py?hl=en.
--
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/web2py?hl=en.