SQLFORM.grid edit form restrictions

157 views
Skip to first unread message

Ivo

unread,
Dec 3, 2013, 4:55:44 PM12/3/13
to web...@googlegroups.com
Hi,
I have want to set some restrictions on editable fields in a edit form grid.

This is the form
def product():
    grid = SQLFORM.grid(db.product, 
        columns=[
                 db.product.name,
                 db.product.description,
                 db.product.price,
                 ],)

when I try to add:
    if request.args[-3] == 'edit':
        db.product.price.writable=False
I get an error 

<type 'exceptions.IndexError'> list index out of range

That traces to the if statement.
however is I load the grid form, and then apply the change and save, the statement works as expected.

I also tried adding:
    else:
        pass
but it doesn't help.

what am I doing wrong or not doing?


黄祥

unread,
Dec 3, 2013, 6:25:06 PM12/3/13
to web...@googlegroups.com
please try not tested :
edit_in_form = 'edit' in request.args
db.product.price.writable = not edit_in_form 

or

edit_in_form = request.args[-3] == 'edit'
db.product.price.writable = not edit_in_form 

best regards,
stifan

Dave S

unread,
Dec 3, 2013, 6:38:18 PM12/3/13
to web...@googlegroups.com
I'd expect the 2nd suggestion to be problematic in the same way as the OP's formulation, if request.args doesn't have the expected len.
maybe a diagnostic print/log to examine the size?

/dps
 

ivo.ni...@gmail.com

unread,
Dec 3, 2013, 6:52:12 PM12/3/13
to web...@googlegroups.com
I tried the sugested methods prior to posting because I came across a post which had that solution in it. It doesn't throw the error, but ignores it totally.
I think the issue is because the url of the form is 
App/default/product/
And the edit function creates a url like
App/default/product/edit/(record id)?(hmac)

I'm not quite sure how to get the diagnostic print/logs.
I'm kind of new to w2p and py in general

Thanks for the replies so far

Sent from my phone
--
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/0yA3-T9MesY/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.

黄祥

unread,
Dec 3, 2013, 7:11:25 PM12/3/13
to web...@googlegroups.com
App/default/product/edit/(record id)?(hmac)

if my logic is correct the edit is args so :
edit_in_form = 'edit' in request.args
or
edit_in_form = request.args[-3] == 'edit'
should work

are you sure? i've tested right now, both is work in mine. tested in web2py 2.8.2 windows binary on windows 7.

best regards,
stifan

Dave S

unread,
Dec 3, 2013, 7:28:05 PM12/3/13
to web...@googlegroups.com

On Tuesday, December 3, 2013 3:52:12 PM UTC-8, Ivo wrote:
I tried the sugested methods prior to posting because I came across a post which had that solution in it. It doesn't throw the error, but ignores it totally.
I think the issue is because the url of the form is 
App/default/product/
And the edit function creates a url like
App/default/product/edit/(record id)?(hmac)

I'm not quite sure how to get the diagnostic print/logs.
I'm kind of new to w2p and py in general

I have to study the logging stuff myself, but using prints depends on whether you've got a console or not.
I'm running on Rocket (the default packaging for Web2Py) from an ordinary terminal instance (hey, I'm just doing an internal tool), and a print statement in my controller function will go  to the web2py console, which is the terminal instance.

/dps
 

Ivo

unread,
Dec 4, 2013, 2:33:09 AM12/4/13
to web...@googlegroups.com
黄祥 you were right!
I fixed the issue just by copy pasting your first solution.
Very stange as I tries a almost identical solution that failed.
I tried something like:
edit_in_form = 'edit' in request.args
db.product.price.readable = db.product.price.writable = not edit_in_form

your second solution fails though I think it's because the request.args[-3] == 'edit' is always false and edit is not contained in de normal url it's only [-3] after you click the edit button. edit doesn't exist until then.

the thing I don't understand is why it errors out on me. it should just pass it when the condition is False.

At least I got it to work thank you so much! 

黄祥

unread,
Dec 4, 2013, 3:00:23 AM12/4/13
to web...@googlegroups.com
i think you can figure it out by yourself where the 'edit' is passed in request.args. the minus part of list is tell that you must sort it backward (from right), so if it : request.args[-3] == 'edit'
the edit must be the 3rd args from the right part 

for your case e.g.
App/default/product/edit/(record id)?(hmac)
so the request.args[-2] == 'edit'

for the suggestion i gave to you above is taken from my code that is using sqlform.smartgrid and in your case you are using sqlform.grid, so please try request.args[-2] == 'edit'.

best regards,
stifan

Ivo

unread,
Dec 4, 2013, 6:13:50 AM12/4/13
to web...@googlegroups.com
Thanks Stifan

do you by any chance know how I can manipulate field values for the edit view, for example change the form labels and field values if they are read only? 

黄祥

unread,
Dec 4, 2013, 6:57:26 AM12/4/13
to web...@googlegroups.com
i think you can achieve it by improve the above example.
e.g. not tested

edit_in_form = 'edit' in request.args

if not edit_in_form:
    db.product.price.label = T('Price Uneditable') 
else:
    db.product.price.label = T('Price Editable') 

best regards,
stifan
Reply all
Reply to author
Forward
0 new messages