If you do not have many authors (if they fix in a dropdown) you can do
db.define_table('book',...,Field('authors','list:reference author'))
and use the plugin_mupliselect that ships with admin.
Otherwise you cannot think of a single way to do it with a single form.
I would have two pages
1) one contains a search for authors and the option to add options to a
cart via ajax (like a shopping cart)
2) one creates book with a 'list:reference author' that picks up values
from the cart and clears the cart.
then you can embed 1) into 2) via LOAD(...)
It can be done in few lines of code. Something like:
session.cart = session.cart or []
db.define_table('author',Field('name'))
db,define_table('book',Field('title'),Field('authors','list:reference
author',readable=False,writable=False)
def cart():
grid = SQLFORM.grid(db.author,links=[lambda row:
A('add',callback=URL('add',args=row.id))])
return grid
def add():
if request.env.request_method == 'POST':
id = request.args(0,cast=int)
if not id in session.cart: session.cart.append(id)
return 'added'
def create_book():
db.book.authors.default=session.cart
form = SQLFORM(db.book)
cart = LOAD(request.controller,'cart')
return dict(form=form,cart=cart)
On Wednesday, 17 July 2013 01:14:27 UTC-5, Andrey Oleynik wrote:
>
> Hi!
>
> I want to submit more than one record to a table from the form.
>
> e.g. i have the following tables in the db:
>
> db.define_table('book',
>
> Field <http://127.0.0.1:8000/examples/global/vars/Field>('title')*)
> **
> *db.define_table('author',
>
> Field <http://127.0.0.1:8000/examples/global/vars/Field>('name')
> Field <http://127.0.0.1:8000/examples/global/vars/Field>('book',
> 'reference book'))
>
>
> and function:
>
> def submit():
> form=SQLFORM
> <http://127.0.0.1:8000/examples/global/vars/SQLFORM>.factory(db.book,db.author)
> if form.process().accepted:
> id = db.book.insert(**db.book._filter_fields(form.vars))
> form.vars.book=id
> id = db.author.insert(**db.author._filter_fields(form.vars))
> response
> <http://127.0.0.1:8000/examples/global/vars/response>.flash='Thanks for
> filling the form'
>
> return dict(form=form)
>
> As result of submitting this form I gets by one record in each tables.
> How could I add more than one author of the book from the form without
> adding the authors in advance?
>
> Thanks
>
--
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.