Massimo, thank you for reply.
Initially I thought about adding input fields to the form like
here<http://infotuts.com/dynamically-add-input-feilds/> and
then save their values to table authors in the loop right after saving the
rest part of the form to table book.
среда, 17 июля 2013 г., 16:22:10 UTC+4 пользователь Massimo Di Pierro
написал:
>
> 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.