SQLFORM.grid change form vars

274 views
Skip to first unread message

Vinyl Darkscratch-Kazotetsu

unread,
Mar 15, 2016, 1:10:48 AM3/15/16
to web2py-users
It doesn't seem like anyone else has run into this particular issue before.  I have an SQLFORM grid that is allowing my staff members on our website to add links to their profile upon the crew page.  An example is mine at https://www.nightwave.me/crew?member=VinylDarkscratch
However, I found out that SQLFORM.grid does not take a fields parameter like normal SQLFORM does, and as such staff members have access to edit fields they should not be able to, such as the user ID.  So, I attempted to set a default for the fields, in the controller function, to be the value that I'd like it to be, however I had gotten the following error:

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
Traceback (most recent call last):
File "/home/vinyldarkscratch/nightwave/gluon/restricted.py", line 227, in restricted
exec ccode in environment
File "/home/vinyldarkscratch/nightwave/applications/nightwave/controllers/default.py", line 294, in <module>
File "/home/vinyldarkscratch/nightwave/gluon/globals.py", line 412, in <lambda>
self._caller = lambda f: f()
File "/home/vinyldarkscratch/nightwave/gluon/tools.py", line 3774, in f
return action(*a, **b)
File "/home/vinyldarkscratch/nightwave/applications/nightwave/controllers/default.py", line 68, in submit_bio_links
linkform = SQLFORM.grid((db.user_links.user_id == auth.user.id))
File "/home/vinyldarkscratch/nightwave/gluon/sqlhtml.py", line 2264, in grid
onsuccess=oncreate)
File "/home/vinyldarkscratch/nightwave/gluon/html.py", line 2301, in process
self.validate(**kwargs)
File "/home/vinyldarkscratch/nightwave/gluon/html.py", line 2238, in validate
if self.accepts(**kwargs):
File "/home/vinyldarkscratch/nightwave/gluon/sqlhtml.py", line 1711, in accepts
self.vars.id = self.table.insert(**fields)
File "/home/vinyldarkscratch/nightwave/gluon/packages/dal/pydal/objects.py", line 712, in insert
ret = self._db._adapter.insert(self, self._listify(fields))
File "/home/vinyldarkscratch/nightwave/gluon/packages/dal/pydal/objects.py", line 658, in _listify
'Table: missing required field: %s' % name)
RuntimeError: Table: missing required field: user_id

Any suggestions on how to set a field's value in an SQLFORM.grid after submission?

Anthony

unread,
Mar 15, 2016, 1:22:27 AM3/15/16
to web2py-users
You can use formargs, editargs, createargs, and viewargs to pass arguments to the SQLFORM calls for create/edit/view forms. You can also set the readable/writable attributes of individual fields to prevent them from being displayed in the forms.

Anthony

Vinyl Darkscratch-Kazotetsu

unread,
Mar 15, 2016, 9:18:00 PM3/15/16
to web2py-users
The user_id field is both non-readable and non-writable already, which is what's causing that error.  It's not receiving any data for the field from anywhere.  As for the various argument passing parameters, I am unsure of how to implement it into the website to set form.vars.user_id to auth.user.id, would you please elaborate?

Anthony

unread,
Mar 16, 2016, 12:24:56 AM3/16/16
to web2py-users
On Tuesday, March 15, 2016 at 9:18:00 PM UTC-4, Vinyl Darkscratch-Kazotetsu wrote:
The user_id field is both non-readable and non-writable already, which is what's causing that error.  It's not receiving any data for the field from anywhere.  As for the various argument passing parameters, I am unsure of how to implement it into the website to set form.vars.user_id to auth.user.id, would you please elaborate?

Note, the error you are seeing is unrelated to the issue of how to hide particular fields from your users in the forms. If you've already set user_id to be non-writable, there is no need for the "fields" argument to SQLFORM. The problem you are having is due to the fact that the field is required but you have excluded it from the form without specifying a default. So, if the default should be auth.user_id, just do:

    Field('user_id', 'reference auth_user', default=auth.user_id)

Anthony

Vinyl Darkscratch-Kazotetsu

unread,
Mar 16, 2016, 1:32:26 AM3/16/16
to web...@googlegroups.com
I see it now — I had assumed that you could set the default in the controller function, but it seems that it has to be in the model file.  Thank you for your help on this issue.

Still, though…I would like to know how to edit the variables submitted by the form, and still unsure how to do that.  For example, if a staff member were to post their Inkbunny link, I would want to force that link’s explicit field to true, but by default they provide the choice of marking it as explicit content or not.  Any good suggestions there?

--
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/I9Xg4BI5SA8/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.

Anthony

unread,
Mar 16, 2016, 1:42:42 AM3/16/16
to web2py-users
On Wednesday, March 16, 2016 at 1:32:26 AM UTC-4, Vinyl Darkscratch-Kazotetsu wrote:
I see it now — I had assumed that you could set the default in the controller function, but it seems that it has to be in the model file.  Thank you for your help on this issue.

No, default can be set after the model declaration, including in the controller (but before the grid is created, as the grid method handles the form processing and database insert, so any model attributes must be set before calling the grid).
 
Still, though…I would like to know how to edit the variables submitted by the form, and still unsure how to do that.  For example, if a staff member were to post their Inkbunny link, I would want to force that link’s explicit field to true, but by default they provide the choice of marking it as explicit content or not.  Any good suggestions there?

Not exactly clear what you want to do, but maybe an onvalidation function would work (it takes the form object, so you can access and manipulate form.vars).

Anthony

Vinyl Darkscratch-Kazotetsu

unread,
Mar 16, 2016, 3:43:55 PM3/16/16
to web...@googlegroups.com
Ah, I see what my problem is — the default was defined after the SQLFORM.grid() in the controller, not before it.  Works like a charm, no errors or issues.

As for onvalidation, I’ll give it a whirl and see how it goes.  Thank you.

icodk

unread,
May 31, 2017, 6:37:53 PM5/31/17
to web2py-users
Antony
could you elaborate or give an example of how to use formargs,editargs etc. ?
You write that it can be used by passing it to SQLFORM but what could be the actual use of it?
In another post 

Niphlod get a bit closer to a use example by specifying  editargs to reorder fields on an edit form. For the first it works  on grid but not on smartgrid, why ?
Secondly, where can I read about what is valid and how it will effect the edit/create form ?  Could I use it to pass data to formstyle ? if yes then how to use it in the formstyle function ?

Thanks in advance

Anthony

unread,
Jun 1, 2017, 1:06:13 PM6/1/17
to web2py-users
On Wednesday, May 31, 2017 at 6:37:53 PM UTC-4, icodk wrote:
Antony
could you elaborate or give an example of how to use formargs,editargs etc. ?

When viewing/creating/updating individual records, the grid creates a SQLFORM. You can use formargs, etc. to pass arguments directly to the SQLFORM instance generated by the grid -- just put the relevant arguments in a dictionary.
 
You write that it can be used by passing it to SQLFORM but what could be the actual use of it?
In another post 

Niphlod get a bit closer to a use example by specifying  editargs to reorder fields on an edit form. For the first it works  on grid but not on smartgrid, why ?

The smartgrid can display forms for multiple tables, so many of its arguments should actually be dictionaries with individual table names as the keys, and then the arguments for each table as the values. So, for smartgrid, you would have something like:

SQLFORM.smartgrid(..., formargs={'table1': dict(...), 'table2': dict(...), ...})

Anthony

icodk

unread,
Jun 1, 2017, 5:52:08 PM6/1/17
to web2py-users
Thanks, That explain why it didn't work in smartgrid (because I haven't specify the table name)
I still do not understand how my arguments will effect the auto generated SQLFORM or what kind of argument will effect the form and in what way.
Is Niphlod's example is the only thing  you can do with it (reorder fields)?
Thanks

Dave S

unread,
Jun 1, 2017, 9:19:02 PM6/1/17
to web2py-users


On Thursday, June 1, 2017 at 2:52:08 PM UTC-7, icodk wrote:
Thanks, That explain why it didn't work in smartgrid (because I haven't specify the table name)
I still do not understand how my arguments will effect the auto generated SQLFORM or what kind of argument will effect the form and in what way.
Is Niphlod's example is the only thing  you can do with it (reorder fields)?
Thanks

Perhaps you could add a link to Niphlod's post, so that we can see what context he was addressing.

/dps
 

Anthony

unread,
Jun 1, 2017, 10:41:39 PM6/1/17
to web2py-users
On Thursday, June 1, 2017 at 5:52:08 PM UTC-4, icodk wrote:
Thanks, That explain why it didn't work in smartgrid (because I haven't specify the table name)
I still do not understand how my arguments will effect the auto generated SQLFORM or what kind of argument will effect the form and in what way.

You can pass any arguments that you would normally pass to SQLFORM(), so they will have whatever effect they would normally have.

Anthony

Dave S

unread,
Jun 2, 2017, 12:37:03 AM6/2/17
to web2py-users


On Thursday, June 1, 2017 at 6:19:02 PM UTC-7, Dave S wrote:


On Thursday, June 1, 2017 at 2:52:08 PM UTC-7, icodk wrote:
Thanks, That explain why it didn't work in smartgrid (because I haven't specify the table name)
I still do not understand how my arguments will effect the auto generated SQLFORM or what kind of argument will effect the form and in what way.
Is Niphlod's example is the only thing  you can do with it (reorder fields)?
Thanks

Perhaps you could add a link to Niphlod's post, so that we can see what context he was addressing.


Whoops, sorry!  You already did this, and I just plain missed it.[...]

On Wednesday, May 31, 2017 at 6:37:53 PM UTC-4, icodk wrote:
[...]

icodk

unread,
Jun 2, 2017, 2:52:02 AM6/2/17
to web2py-users
Dave S.
I appreciate  your responsiveness and alertness and the ever willingness to help others.
Thanks

icodk

unread,
Jun 2, 2017, 2:53:31 AM6/2/17
to web2py-users
Thanks
I am a bit slow but I think I got it.
Reply all
Reply to author
Forward
0 new messages