REF: Displaying a Virtual Field in SQLFORM.grid throws an exception....

813 views
Skip to first unread message

Teddy Nyambe

unread,
Dec 18, 2012, 2:03:05 AM12/18/12
to web2py
Does SQLFORM.grid support adding and display of a virtual field...am getting an exception...my code:

db.file_subject_issue.file_name = Field.Virtual(lambda row: get_file_name(row.file_subject_issue.file_id))
fields = [db.file_subject_issue.file_id, db.file_subject_issue.date_created, db.file_subject_issue.file_name]
query =....

form = SQLFORM.grid(query,fields=fields,...)

I am getting the error

<type 'exceptions.AttributeError'> 'FieldVirtual' object has no attribute '_tablename'


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
Traceback (most recent call last):
File "/home/www-data/web2py/gluon/restricted.py", line 212, in restricted
exec ccode in environment
File "/home/www-data/web2py/applications/intranet/controllers/administration.py", line 176, in <module>
File "/home/www-data/web2py/gluon/globals.py", line 188, in <lambda>
self._caller = lambda f: f()
File "/home/www-data/web2py/applications/intranet/controllers/administration.py", line 71, in file_track
selectable=False,csv=False , paginate=20, user_signature=False)
File "/home/www-data/web2py/gluon/sqlhtml.py", line 1796, in grid
if field._tablename in tablenames]
AttributeError: 'FieldVirtual' object has no attribute '_tablename'



--
.......................................................................................
Teddy Lubasi Nyambe
Opensource Zambia
Lusaka, ZAMBIA

Cell: +260 97 7760473
website: http://www.opensource.org.zm

~/
Human Knowledge belongs to the world! - AntiTrust

Man is a tool-using animal. Without tools he is nothing, with tools he is all - Thomas Carlyle 1795-1881

/~

Massimo Di Pierro

unread,
Dec 18, 2012, 10:32:42 AM12/18/12
to web...@googlegroups.com
Right now grid does not work with virtual fields. There is a pending proposal for enhancement to support them.

Richard Vézina

unread,
Apr 5, 2013, 8:42:04 PM4/5/13
to web2py-users
I fall on this exact problem, I don't see anything about taht in the change log of 2.4.4. Is this will be fix soon? I mean SQLFORM.grid support for virtual field

THanks

Richard


--
 
 
 

Massimo Di Pierro

unread,
Apr 6, 2013, 12:33:01 PM4/6/13
to web...@googlegroups.com
I will be fixed asap. I cannot give you a deadline. 

Richard Vézina

unread,
Apr 9, 2013, 4:47:32 PM4/9/13
to web2py-users
No problem, thanks for the answer!

Richard


--
 
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jurgis Pralgauskis

unread,
Apr 21, 2013, 2:49:36 PM4/21/13
to web...@googlegroups.com
Hi, probably, virtual fields can be injected somewhere in:




ps.: For virtual fields, one should know which other fields of table it depends on -  to .select(..) them for calculations.
that might need some changes when defining VirtualField ?
and but these other fields might not need be shown in grid..., so should be apart from fields=... argument.

ps.: VirutalFields doesn't have .tablename property - not implemented yet, or other reasons?

Jurgis Pralgauskis

unread,
Apr 21, 2013, 3:11:44 PM4/21/13
to web...@googlegroups.com
for now the workaround might be manipulating generated html table 

you need to know id of records - so you could get info for calculating virtual field, 

so instead :
fields=[db.product.name, db.product.cost, db.product.quantity, db.product.virtual_total]

give 
fields=[db.product.name, db.product.cost, db.product.quantity, db.product.id]

and then replace/calculate the 4th column contents:

grid = SQLFORM.grid(query or table, fields=fields)
trs = grid.element("table").elements("tr")
for tr in grid.element("tbody").elements("tr"):
    id = int(tr[3][0]) 
    tr[3][0] =  str(   db.product(id).virtual_total ) 



ps.: for easier manipulation could use dict notation:
db.product['virtual_total']
db.product(id)['virtual_total']

Jurgis Pralgauskis

unread,
May 8, 2013, 4:45:17 PM5/8/13
to web...@googlegroups.com
By the way, 

I found http://www.web2py.com/book/default/chapter/06#before-and-after-callbacks

which can help in some cases, where I thought to use virtual fields

as with callbacks you can update needed info from foreign tables (or computed fields might also be enough in some cases)

tospecht

unread,
Jul 18, 2013, 6:19:00 PM7/18/13
to web...@googlegroups.com
Hi,

i have just created a small patch to get virtual fields working with SQLFORM.grid.
Let me know when it doesn't work for you.
I have tested it only with my mysql db and without db options like left join, group, etc ..
A pull request is on the way.

Have fun using virtual field in you grid!
virtual_fields.patch

Massimo Di Pierro

unread,
Jul 19, 2013, 4:27:50 AM7/19/13
to web...@googlegroups.com
Your code is in trunk with some minor refactoring. Should work as in your patch. Can you please help me check it? Thanks

peckto

unread,
Jul 19, 2013, 5:55:20 PM7/19/13
to
Regarding your changes:
+            all_fields = filter(lambda nv: nv[1] in fields and
+                                isinstance(nv[1],(FieldVirtual,FieldMethod)),
+                                table.iteritems())            
It doesn't work. It ends up in returning all virtual fields, even when they are not requested.
It's because of the "var in list" statement:
nv[1] in fields
always returns True. I don't know why.

Another solution would be to use the id() function:
id(nv[1]) in [id(f_) for f_ in fields]
but you have overwritten this function
2091: id = value

So I came finally to the complicated looking statement:
nv[0] in [f_.name for f_ in fields] and nv[1].tablename == tablename

Maybe you can tell me why the "var in list" statement fails
or we use one of the other solutions (while renaming the id value).

Massimo Di Pierro

unread,
Jul 20, 2013, 3:00:52 AM7/20/13
to web...@googlegroups.com
Strange. Ok. I changed it again. Can you please check it. I am still trying to avoid the double loop for every virtual table.

peckto

unread,
Jul 20, 2013, 5:54:24 AM7/20/13
to web...@googlegroups.com
I think i can explain this behaviour.
The statement "var in list" cals the lists __contains__ method,
which loops through the list and compares each element with var.
But in case of an Field object the equals operator (__eq__) has another meaning,
it's the "WHERE" part of the db query:
db(db.table.id==1).select()
So this comparison will olways return True.

Adam Filić

unread,
Jul 23, 2014, 10:41:54 AM7/23/14
to web...@googlegroups.com
One year later I still can't display virtual field in the SQLFORM.grid with an error:

<type 'exceptions.AttributeError'> 'Row' object has no attribute 'broj'

And my virtual field is defined as: 

db.define_table('fin_fis',
    Field('god','integer',label=T('Year')),
    Field('pod','reference sif_pod',label=T('Company'),writable=False, readable=False),
    Field('oj','reference sif_oj',label=T('Department')),
    Field('kasa','integer',label=T('Billing device')),
    Field('vd','reference sif_vd',label=T('Document type')),
    Field('br','integer',label=T('Number')),
    Field.Virtual('broj',lambda  row: '%05d/P%03d%03d/%d'%(row.fin_fis.br,row.fin_fis.oj,row.fin_fis.vd,row.fin_fis.kasa),label='Broj'),
    .....
    )

This is my field list:
fields = (db.fin_fis.oj,db.fin_fis.kasa,db.fin_fis.br,db.fin_fis.broj,db.fin_fis.dvi,db.fin_fis.iznrac,db.fin_fis.npl,db.sif_radnici.ime, db.sif_radnici.prezime)

I'm using:  2.9.5-stable+timestamp.2014.03.16.02.35.39 (Running on Apache/2.4.9 (Ubuntu), Python 2.7.3)

Anthony

unread,
Jul 23, 2014, 11:53:20 AM7/23/14
to web...@googlegroups.com
I think the problem is that your virtual field depends on some fields you have not included in your grid, so the virtual field values cannot be calculated. Instead of specifying the "fields" argument, you can hide particular fields from the grid by setting their "readable" attribute to False. In that case, all the fields will be included in the query done by the grid, which will enable the virtual field values to be calculated.

Anthony

Adam Filić

unread,
Jul 23, 2014, 12:44:30 PM7/23/14
to web...@googlegroups.com
Yes, you are right. That was the problem and it shows up in the grid now, but I can't see it in the view form. All included fields are readable.
Thank you


--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/yIRGzpZYcbg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Srdačan pozdrav

Adam Filić

www.tt-program.com

Anthony

unread,
Jul 23, 2014, 1:41:31 PM7/23/14
to web...@googlegroups.com
True, readonly forms do not show virtual fields. Perhaps submit a Google Code issue requesting that feature.

Anthony
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages