Fields being set to NULL on SQLFORM.grid update

103 views
Skip to first unread message

Jim S

unread,
Jun 13, 2013, 5:32:34 PM6/13/13
to web...@googlegroups.com
It is possible that I'm losing my mind, but it appears to me that when I submit a SQLFORM.grid update with a custom form that fields not included in the custom form are being set to NULL even if they had a value in them before.

I've just noticed this after updating to 2.5.1 stable.

-Jim

Massimo Di Pierro

unread,
Jun 13, 2013, 11:46:58 PM6/13/13
to web...@googlegroups.com
Can we see an example?

Jim S

unread,
Jun 14, 2013, 9:54:59 AM6/14/13
to web...@googlegroups.com
Here you go.  Attached.

-Jim
web2py.app.grid_update.w2p

Jim S

unread,
Jun 14, 2013, 11:52:48 AM6/14/13
to web...@googlegroups.com
I just reverted back to 2.4.6-stable+timestamp.2013.05.10.08.32.00 and the problem exists there as well.

-Jim

Massimo Di Pierro

unread,
Jun 16, 2013, 4:41:16 AM6/16/13
to web...@googlegroups.com
The problem is that the grid expects the field to be writable in the form. You do not include them therefore it thinks they are empty (html convention, not web2py's).

Assuming you want the fields in the "create" form but not in the edit form you can replace:

    grid = SQLFORM.grid(db.person.id>0,
                        fields=[db.person.first, db.person.last])

with

    if request.args(0) == 'edit':
        db.person.song.writable = False
        db.person.tv_show.writable = False
    grid = SQLFORM.grid(db.person.id>0,
                        fields=[db.person.first, db.person.last])

Jim Steil

unread,
Jun 16, 2013, 4:02:28 PM6/16/13
to web...@googlegroups.com

I was hoping I wouldn't have to do that but I understand.  I think it should be pointed out in the manual that this needs to be done so others don't make the same mistake I did.

Thanks Massimo, I really appreciate the reply.

Jim

--
 
---
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/EiAy_w4BLxY/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/groups/opt_out.
 
 

Massimo Di Pierro

unread,
Jun 16, 2013, 4:32:01 PM6/16/13
to web...@googlegroups.com
How do you think it should work? If we can make it better we will.

Jim Steil

unread,
Jun 16, 2013, 4:35:27 PM6/16/13
to web...@googlegroups.com

I thought I could just use whatever fields I wanted and it would figure out which ones to update.  The way it is now, if I add a field to a table and it isn't included in the writable = False statements the It is going to be set to null on any future updates.  I would prefer to not have to worry about updating all custom forms if I should add a field to a table sometime in the future.

Jim

Massimo Di Pierro

unread,
Jun 17, 2013, 1:53:31 AM6/17/13
to web...@googlegroups.com
I am afraid this is not possible because HTML cannot distinguish an empty submission from no submission. Somehow we have to tell that explicitly to web2py. Perhaps there is a better way to do it but I do not see on. :-(

Jim Steil

unread,
Jun 17, 2013, 9:31:17 AM6/17/13
to web...@googlegroups.com
Not what I wanted to hear, but that is what I figured was the issue.

However, I think it should be noted in the book that when using SQLFORM.grid and custom forms that any field not included in the custom form will be set to NULL on every update of the form.

Jim S

unread,
Jun 17, 2013, 12:18:12 PM6/17/13
to web...@googlegroups.com
FWIW - My concern for this is primarily that as I add fields to tables months/years from now I may forget to set writable to False for them and therefore they would be set to NULL without my being aware.  To get around this, I've added the following to my controllers that use SQLFORM.grid and call custom forms.

if request.args(0) == 'edit':
    for field in db.table_for_grid.fields:
        db.table_for_grid[field].writable = False

    db.table_for_grid.field_to_edit_1.writable = True
    db.table_for_grid.field_to_edit_2.writable = True
    ...etc...

grid = SQLFORM.grid(db.table_for_grid.id > 0)

-Jim
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages