Multiple Instances of a table in a web2py form
Trying to make a poll application in web2py.
Model
db.define_table('t_query',
Field('f_content', type='te开发者_运维知识库xt', requires=IS_NOT_EMPTY(),
label=T('Content')),
SQLField('is_active',db.auth_user,writable=False,readable=False),
auth.signature,
migrate=settings.migrate)
db.define_table('t_option',
Field('f_ocontent', type='string',requires=IS_NOT_EMPTY(),
label=T('Option')),
auth.signature,
migrate=settings.migrate)
Controller
def ask():
form=SQLFORM.factory(db.t_query,db.t_option,db.t_option)
if form.process().accepted:
id = db.t_query.insert(**db.t_query._filter_fields(form.vars))
form.vars.client=id
id = db.t_option.insert(**db.t_option._filter_fields(form.vars))
response.flash='Thanks for filling the form'
id = db.t_option.insert(**db.t_option._filter_fields(form.vars))
response.flash='Thanks for the question'
return dict(form=form)
View
{{extend 'layout.html'}}
<h2>Start a Poll</h2>
{{=form}}
Output
I have tried finding how to ask SQLFORM to use fields from the same table multiple times in a form.
This is what I expect:
How do I go about it?
Asked the same question on web2py@googlegroups https://groups.google.com/forum/#!topic/web2py/48tO5ncC2t4
It sounds like you want to be able to store a list of strings for a single field. As far as I know, you cannot repeat the same field multiple times (where would the data go in the database? There is only a single field to contain it, so you need to serialize it and jam it in there if you want 0..n values in a single field)
Use this to get multiple strings in a single field:
db.define_table(...
...
Field('f_ocontent', 'list:string')
)
This will store the data internally as such:
|option1val|option2val|option3val|
or, if empty:
||
You can hit enter when you're in an input that has more than 0 chars and it will create a new input.
精彩评论