virtual fields

25 views
Skip to first unread message

mr.freeze

unread,
Oct 31, 2009, 2:16:42 PM10/31/09
to web2py-users
Virtual fields are my new favorite thing. Currently you must call
setvirtualfields after each select. What about adding the ability to
setvirtualfields at the Table level so that any select will call
setvirtualfields automatically? Pseudocode:

db.define_table('mytable',Field('cost'),Field('quantity'))
class ComputedFields:
def amount(self):
return self.mytable.cost*self.mytable.quantity
db.mytable.setvirtualfields(total=ComputedFields())

What do you think?

Douglas Soares de Andrade

unread,
Oct 31, 2009, 3:00:57 PM10/31/09
to web...@googlegroups.com
Em Sat, 31 Oct 2009 11:16:42 -0700 (PDT)
"mr.freeze" <nat...@freezable.com> escreveu:

Hi,

+1 for me, i liked this syntax and it seems very easy to understand.

mdipierro

unread,
Oct 31, 2009, 4:04:32 PM10/31/09
to web2py-users
I like it but with some caveats. In

rows.setvirtualfields(total=ComputedFields())

we need to specify total to clarify the table or virtualtable the
virtualfields are attached to.
when attaching virtualfields to a table there is no need to specify
that.

So I think it should just be:

table.addvirtualfields(ComputeFields())

Implementation:

The argument would be appended to

table._virtualfields=[]

and the latter would be precessed by

Set.parse

that for each element table._virtualfields would call

setvirtualfields

mdipierro

unread,
Oct 31, 2009, 4:31:46 PM10/31/09
to web2py-users
or just

db.table.virtualfields.add(ComputedFields())

as a lazy

rows.setvirtualfields(table=ComputedFields())

it can be implemented in two lines if ok.

Massimo

mr.freeze

unread,
Oct 31, 2009, 4:45:10 PM10/31/09
to web2py-users
virtualfields.append not add, right? Sounds good to me. Shall I take
a crack at it or do you already have it in trunk :)

Joe Barnhart

unread,
Oct 31, 2009, 4:45:24 PM10/31/09
to web...@googlegroups.com
I admit I'm not "up" on virtual fields.  I didn't notice them when they were added.  I'm wondering if they carry the extra info that fields have, such as a default widget to display the field?  (Obviously if the fields are computed there are no validators.)

mdipierro

unread,
Nov 1, 2009, 12:39:25 AM11/1/09
to web2py-users
I have done it. Post it soon. Yet, it is append.

mdipierro

unread,
Nov 1, 2009, 12:40:05 AM11/1/09
to web2py-users
They only have values. widgets and validators in web2py are only for
input.

On Oct 31, 3:45 pm, Joe Barnhart <joe.barnh...@gmail.com> wrote:
> I admit I'm not "up" on virtual fields.  I didn't notice them when they were
> added.  I'm wondering if they carry the extra info that fields have, such as
> a default widget to display the field?  (Obviously if the fields are
> computed there are no validators.)
>

mr.freeze

unread,
Nov 1, 2009, 4:01:39 PM11/1/09
to web2py-users
Tested and working. It threw me off because the virtual fields don't
show up in BEAUTIFY(rows) for some reason. Thanks.

mdipierro

unread,
Nov 1, 2009, 4:25:16 PM11/1/09
to web2py-users
True they do not. That is a complicated matter.

BEAUTIFY tries serialize rows by calling rows.xml() which calls
SQLTABLE(rows) and that only display rows.colnames where colnames are
returned by the DB, not computed.

I am not convinced this behavior should be changed since there is no
way to enable/disable columns potentially associated to virtual fields
and there are going to be more virtual fields than one wants to
display.

Massimo

mr.freeze

unread,
Nov 1, 2009, 4:48:47 PM11/1/09
to web2py-users
That makes sense. Do you think virtual fields should have a represent
attribute?

mdipierro

unread,
Nov 1, 2009, 5:26:50 PM11/1/09
to web2py-users
I think no. They are their own representation.
Reply all
Reply to author
Forward
0 new messages