Model
db.define_table('client',
Field('name'))
db.define_table('address',
Field('client',db.client,writable=False,readable=False),
Field('street'),Field('city'))
Controller
def register():
form=SQLFORM.factory(db.client,db.address)
if form.accepts(request.vars):
id = db.client.insert(**db.client._filter_fields(form.vars))
form.vars.client=id
id = db.address.insert(**db.address._filter_fields(form.vars))
response.flash='Thanks for filling the form'
return dict(form=form)
Notice the SQLFORM.factory (it makes ONE form using public fields from both tables and inherits their validators too).
On form.accepts this does two inserts (some data in one table and some data in the other).
Massimo
Comments (6)
- Login to post
order by: newest oldest upvoted downvoted
Great and useful slice. I was wondering how to do linked forms and now seems easier and fits very well with the Model-View-Controller design.
Thank you.
Very useful.
How can this be extended for updating/deleting? I keep getting errors when I try the following (as per SQLFORM docs):
def update():
record = db.client.id[1]
form = SQLFORM.factory(db.client, db.address, record)
Brilliant! just what I needed
This code not work:
def update():
record = db.client.id[1]
form = SQLFORM.factory(db.client, db.address, record)
This error:
SyntaxError: define_table argument is not a Field or Table: SUBSTR(grupos.id,2,(3 - 2))
Its working nicely, but I found that if I try to use it to extend the out of the box auth, I lose all the build in functionality. e.g if I want to add multiple address as lined table
form = SQLFORM.factory(db.auth_user, db.address)
It looks as if I must follow this example, I will have to rewrite myself all the registration logic such as password_line2, email verification, etc.
Is it true? or is there a solution to achieve the above while still keeping the out of box auth functionality?
show more comments0
murphy 14 years ago
0
cfhowes 14 years ago
0
roaldosinga 13 years ago
0
gilsondev 13 years ago
0
yuvall 13 years ago