SQLFORM factory with multiple correlated tables

56 views
Skip to first unread message

csantos

unread,
May 13, 2012, 8:30:41 PM5/13/12
to web...@googlegroups.com
Hi,
Suppose I have two tables, with a one-to-one relationship:

db.define_table('user', Field('name'));
db.define_table('car', Field('color'), Field('user_id', db.user))

What is the easiest way to create an SQLFORM that would store correlated data in both fields? For instance, create a user with name Steve who owns a red car, that would create two registries (name=Steve), (color=red, user_id=<Steve's id>). As far as I'm concerned, I could do something like this:

form=SQLFORM.factory(db.user, db.car)
if form.process().accepted:
    # for what I've tested, only the user was inserted, so I have to insert the car manually
    if db.car.insert(color=request.vars.color, user_id=form.vars.id):
        response.flash(T('Eh nois que voa'))

But I'm not sure this is the best approach (in terms of security, performance and software engineering). Any insights?
Thanks

csantos

unread,
May 15, 2012, 10:39:33 AM5/15/12
to web...@googlegroups.com
Update: This feature is actually documented in http://web2py.com/book/default/chapter/07#One-form-for-multiple-tables, what a shame I didn't see it earlier...

Regards
Reply all
Reply to author
Forward
0 new messages