virtual fields and grid

69 views
Skip to first unread message

David Manns

unread,
Apr 3, 2023, 8:31:39 AM4/3/23
to py4web
I have published a simple app that demonstrates these behaviors at: https://github.com/davidmanns/virtualfields.git

in the model:
db.define_table('people',
    Field('first'),
    Field('last'),
    Field.Virtual('name', lambda r: f"{r['first']} {r['last']}")
)
controller:
@action('grid', method=['POST', 'GET'])
@action('grid/<path:path>', method=['POST', 'GET'])
@action.uses("grid.html", db, session, flash)
def grid(path=None):
    grid = Grid(path, db.people.id>0,
            orderby=db.people.last|db.people.first,
            columns=[db.people.name, db.people.first, db.people.last],
            grid_class_style=GridClassStyleBulma,
            formstyle=FormStyleBulma,
            )
    footer = CAT(P("following shows results of db(db.people.id>0).select() BEAUTIFY'd"),
                P(BEAUTIFY(db(db.people.id>0).select())),
                P("following shows results of db.people[1], BEAUTIFY'd"),
                P(BEAUTIFY(db.people[1]))
                )
    return locals()


result screenshot is attached.

1. in the grid "edit" form for a record, the virtual field is displayed as read-only. In web2py's grid, virtual fields are not displayed in the edit form. In py4web set the virtual field readable=False removes it from the edit form, but also prevents it from being displayed as a column in the grid.

2. more seriously, contrary to what it explicitly says in the documentation, virtual fields are not " automatically computed for all records when the records are selected". This is also contrary to what happens in web2py.

3. another problem I observed with this simple app is that the record 'delete' button fails. Clicking OK on the confirmation popup produces a failure:
Traceback (most recent call last): File "C:\Users/David/SkyDrive/py4web\py4web\core.py", line 930, in wrapper ret = func(*func_args, **func_kwargs) File "C:\Users/David/SkyDrive/py4web\py4web\core.py", line 915, in wrapper raise context["exception"] File "C:\Users/David/SkyDrive/py4web\py4web\core.py", line 894, in wrapper context["output"] = func(*args, **kwargs) File "C:\Users\David\SkyDrive\py4web\apps\virtualfields\controllers.py", line 45, in grid grid = Grid(path, db.people.id>0, File "C:\Users/David/SkyDrive/py4web\py4web\utils\grid.py", line 479, in __init__ self.process() File "C:\Users/David/SkyDrive/py4web\py4web\utils\grid.py", line 668, in process if referred and referrer.query: NameError: name 'referred' is not defined

One can delete a record using the edit button and clicking the delete record checkbox (in this case there is no confirmation popup, which is also different from web2py)

screenshot-127.0.0.1_8000-2023.04.03-08_29_00.png

Jim Steil

unread,
Apr 3, 2023, 9:28:54 AM4/3/23
to py4web
Have you pulled the latest py4web from github?

1. If you want it readable=False in the form but visible in the grid you could use a custom column definition to display it.
2. To me this seems to be a pyDAL issue. But, if web2py handles it and py4web doesn't, then I'm guessing web2py has additional code to make it work. But, I don't know anything about the internals of either of those packages.
3. This should be fixed (in github) with the merge Massimo did over the weekend.  I don't know if it made it to pypy yet.

-Jim

David Manns

unread,
Apr 3, 2023, 10:11:24 AM4/3/23
to py4web

I thought I was up to date but just did a py4web 'git pull' and picked up some changes. I work in VSCode with a source code  installation of py4web.

These changes indeed fixed the 'delete' button problem, my 3.

My 1 and 2 are unaffected.

On 1, yes I had planned to use custom columns (or live with the fields being displayed in the edit form) if necessary, but nevertheless I think it would be nice if grid was consistent with web2py's grid in this respect.

On 2, I agree it would seem to be a pyDAL issue, but the behavior differences suggest there is code within web2py and py4web involved. And the statement I quoted from the Virtual Fields section of the docs is certainly not correct. In fact there's not much point to virtual fields if they are not computed when the records are retrieved, so I certainly think this is a bug. Not a bug specifically in grid but rather somewhere in the core, I'm guessing.

-David

Jim Steil

unread,
Apr 3, 2023, 10:21:20 AM4/3/23
to py4web
David

I think it's obvious I'm not going to be able to help so I'll get out of this conversation.  Hopefully Massimo will have some time to look at it.

-Jim
Reply all
Reply to author
Forward
0 new messages