SQLFORM keepvalues - only 1 field

42 views
Skip to first unread message

Jim S

unread,
Apr 30, 2020, 3:10:42 PM4/30/20
to web2py-users
Does anyone know how I can have a form 'keepvalues' but only keep one specific field value?  I don't want it to keep the values for all the fields, just for the one specific field.

Anyone tried this before?

-Jim

Scott Hunter

unread,
Apr 30, 2020, 4:48:44 PM4/30/20
to web2py-users
I recall doing this by "pre-populating" the field you want to keep with the old value.

- Scott

Jim Steil

unread,
Apr 30, 2020, 4:54:02 PM4/30/20
to web...@googlegroups.com
I was hoping to avoid those tricks.  I too have done this by storing values in the session to be redisplayed.  I was hoping that the keepopts argument to SQLFORM would allow you to specify which fields to keep, but it seems that is used for something else.

--
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/tgTvG8OqWso/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/b4575285-7562-4a55-9470-0adfcbe354f1%40googlegroups.com.

Scott Hunter

unread,
Apr 30, 2020, 5:19:11 PM4/30/20
to web2py-users
Since the value in question is part of the current submission, isn't the value available in form.vars?

- Scott


On Thursday, April 30, 2020 at 12:54:02 PM UTC-4, Jim S wrote:
I was hoping to avoid those tricks.  I too have done this by storing values in the session to be redisplayed.  I was hoping that the keepopts argument to SQLFORM would allow you to specify which fields to keep, but it seems that is used for something else.

On Thu, Apr 30, 2020 at 11:48 AM Scott Hunter <shu...@nycap.rr.com> wrote:
I recall doing this by "pre-populating" the field you want to keep with the old value.

- Scott

On Thursday, April 30, 2020 at 11:10:42 AM UTC-4, Jim S wrote:
Does anyone know how I can have a form 'keepvalues' but only keep one specific field value?  I don't want it to keep the values for all the fields, just for the one specific field.

Anyone tried this before?

-Jim

--
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/tgTvG8OqWso/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web...@googlegroups.com.

Jim Steil

unread,
Apr 30, 2020, 5:21:53 PM4/30/20
to web...@googlegroups.com
Yes, but I need that value on the next time through the method.  Therefore I have to save it to my session and grab it the next time in.  Am I missing something?

To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/14c77d74-1910-4b05-9b88-fac8bef5cf05%40googlegroups.com.

Scott Hunter

unread,
Apr 30, 2020, 5:30:44 PM4/30/20
to web2py-users
Isn't "the next time through the method" when there is a new submission, which would have the saved value in it (unless the user changed it)?  Maybe a sample of your code would make things clearer.

Jim S

unread,
Apr 30, 2020, 6:06:46 PM4/30/20
to web2py-users
Here is what I'm talking about:

def update_something():
    fields
= [Field('name'),
             
Field('address', 'text'),
             
Field('city'),
             
Field('state'),
             
Field('zip_code')]

    form
= SQLFORM.factory(*fields)

   
if form.process().accepted:
       
#  update the database here

   
return dict(form=form)

After the first time a user updates a record, I want the value of 'state' to remain and everything else go back to the defaults.  I know I can do it by adding some code after form.process().accepted to save the value to the session, and then check it before defining the fields, but 'form' doesn't exist yet.

I still think I must be missing something here....

-Jim

Scott Hunter

unread,
Apr 30, 2020, 7:14:48 PM4/30/20
to web2py-users
Would this do the job?

    form = SQLFORM.factory(*fields)
    if form.process().accepted:
        #  update the database here
        form2 = SQLFORM.factory(*fields)
        form2.vars['state'] = form.vars.state
        form2.process()
    else:
        form2 = form
        
    return dict(form=form2)

Jim S

unread,
Apr 30, 2020, 8:19:19 PM4/30/20
to web2py-users
Wow, that is clever!  And, works just how I want it to.  Thanks so much for the pointer.

-Jim

mostwanted

unread,
May 7, 2020, 6:09:49 PM5/7/20
to web2py-users
Nice one
Reply all
Reply to author
Forward
0 new messages