Problem with a virtual field, SQLFORM.grid and the fields parameter

61 views
Skip to first unread message

Martin Weissenboeck

unread,
Apr 14, 2018, 4:52:10 PM4/14/18
to web2py-users
I have a model with a virtual field:

db.define_table("tt", 
    Field("f"),
    Field("gg"),
    Field.Virtual("ff", lambda r:r.tt.f)
    )

And a grid:

def t():
    form = SQLFORM.grid(
            db.tt,
        )
    return dict(form=form)

Works fine.
But with the field argument I get an error:

def t():
    form = SQLFORM.grid(
            db.tt,
            fields=[db.tt.ff]
        )
    return dict(form=form)

Error ticket for "gridtest"

Ticket ID

194.118.190.134.2018-04-14.22-40-01.bc6da3b6-bd21-4dbe-8c78-f50c4a349fec

<type 'exceptions.KeyError'>

Version

web2py™Version 2.15.4-stable+timestamp.2017.09.02.04.02.22
PythonPython 2.7.12: /usr/local/bin/uwsgi (prefix: /usr/local)

Traceback

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
Traceback (most recent call last):
File "/home/www-data/web2py/gluon/restricted.py", line 219, in restricted
exec(ccode, environment)
File "/home/www-data/web2py/applications/gridtest/controllers/default.py", line 105, in <module>
File "/home/www-data/web2py/gluon/globals.py", line 414, in <lambda>
self._caller = lambda f: f()
File "/home/www-data/web2py/applications/gridtest/controllers/default.py", line 16, in t
db.tt.ff,
File "/home/www-data/web2py/gluon/sqlhtml.py", line 2886, in grid
value = row[str(field)]
File "/home/www-data/web2py/gluon/packages/dal/pydal/objects.py", line 84, in __getitem__
raise KeyError
KeyError
What is wrong?


Regards Martin


Anthony

unread,
Apr 15, 2018, 2:43:22 AM4/15/18
to web2py-users

But with the field argument I get an error:

def t():
    form = SQLFORM.grid(
            db.tt,
            fields=[db.tt.ff]
        )
    return dict(form=form)

What happens if you remove the "fields" argument and instead limit which fields display by setting their "readable" attributes to False?

Anthony

Martin Weissenboeck

unread,
Apr 15, 2018, 6:47:46 AM4/15/18
to web2py-users
Yes, that is possible.

def t():
    db.tt.f.readable = False
    form = SQLFORM.grid(db.tt)
    return dict(form=form)


But now there is the next problem. I try to use the Export "CSV" button and I get another eooror:

Ticket ID

194.118.190.134.2018-04-15.12-45-01.b4d5db05-e1e0-40b8-9233-47f2a2b37cca

<type 'exceptions.KeyError'> '"tt"."ff"'

Version

web2py™Version 2.15.4-stable+timestamp.2017.09.02.04.02.22
PythonPython 2.7.12: /usr/local/bin/uwsgi (prefix: /usr/local)

Traceback

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
Traceback (most recent call last):
File "/home/www-data/web2py/gluon/restricted.py", line 219, in restricted
exec(ccode, environment
File "/home/www-data/web2py/gluon/globals.py", line 414, in <lambda>
self._caller = lambda f: f
()
File "/home/www-data/web2py/applications/gridtest/controllers/default.py", line 14, in t
form = SQLFORM.grid(db.tt)
File "/home/www-data/web2py/gluon/sqlhtml.py", line 2601, in grid
raise HTTP(200, oExp.export(), **response.headers)
File "/home/www-data/web2py/gluon/sqlhtml.py", line 3648, in export
self.rows.export_to_csv_file(s, represent=True)
File "/home/www-data/web2py/gluon/packages/dal/pydal/objects.py", line 2582, in export_to_csv_file
field = fieldmap[col]
KeyError: '"tt"."ff"'

It seems that the export function does not find the virtual field.
Is there any workaround?

Martin



Anthony

--
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 the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



Anthony

unread,
Apr 15, 2018, 7:51:57 PM4/15/18
to web2py-users
Looks like a bug. Feel free to open a Github issue.

Anthony

icodk

unread,
Apr 16, 2018, 3:28:15 AM4/16/18
to web2py-users
I had a similar issue and using the latest master branch fixed it for me
(after the release of 2.16.1)

Martin Weissenboeck

unread,
Apr 16, 2018, 4:58:46 AM4/16/18
to web2py-users
Thank you, I'll try it!

--
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 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.
Reply all
Reply to author
Forward
0 new messages