Tip of the day

99 views
Skip to first unread message

mdipierro

unread,
Oct 16, 2010, 10:57:52 AM10/16/10
to web2py-users
It often happens that you have two tables (for example 'client' and
'address' which are linked together by a reference and you want to
create a single form that allows to insert info about one client and
its default address. Here is how:

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



Bruno Rocha

unread,
Oct 16, 2010, 11:44:01 AM10/16/10
to web...@googlegroups.com
Nice! Thank you. I am going to use that right now.

Need to create a blog/wiki or some place to document all your tips, tricks and revealed secrets about DAL and web2py.

2010/10/16 mdipierro <mdip...@cs.depaul.edu>

Richard Vézina

unread,
Oct 16, 2010, 4:45:49 PM10/16/10
to web...@googlegroups.com
I was about to ask for the question!!



Thank you Massimo

Richard
330.gif

DenesL

unread,
Dec 5, 2010, 10:41:42 PM12/5/10
to web2py-users

This only works for tables that do not have field names in common
and have no references between them.

mdipierro

unread,
Dec 5, 2010, 11:05:07 PM12/5/10
to web2py-users
No field names in comment yes. References between them is not a
problem as long the reference fields are not writable and readable
when generating the form.

weheh

unread,
Dec 5, 2010, 11:58:20 PM12/5/10
to web2py-users
This is the first time I'm seeing the construct "_filter_fields". I
can guess at what it does, but could you explain, please? I couldn't
find it in the doc.

Also, for clarification, do readable and writable only apply to forms
(seems so)? If a field is writable=False, it seems like it can still
be written explicitly by a db.table.insert. Yes?

On Oct 16, 9:57 am, mdipierro <mdipie...@cs.depaul.edu> wrote:

DenesL

unread,
Dec 6, 2010, 5:32:54 AM12/6/10
to web2py-users

True, only restriction is no common field names.
I have added this to the book.

Carlos

unread,
Dec 12, 2010, 4:33:01 PM12/12/10
to web2py-users
Hi,

I am wondering how this solution (single form with linked tables) can
be used for updates (in addition to creates)?.

Thanks,

Carlos


On Dec 6, 4:32 am, DenesL <denes1...@yahoo.ca> wrote:
> True, only restriction is no common field names.
> I have added this to the book.
>
> On Dec 5, 11:05 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
>
>
>
>
>
>
> > No field names in comment yes. References between them is not a
> > problem as long the reference fields are not writable and readable
> > when generating theform.
>
> > On Dec 5, 9:41 pm, DenesL <denes1...@yahoo.ca> wrote:
>
> > > This only works fortablesthat do not have field names in common
> > > and have no references between them.
>
> > > On Oct 16, 9:57 am, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > > > It often happens that you have twotables(for example 'client' and
> > > > 'address' which arelinkedtogether by a reference and you want to
> > > > create asingleformthat allows to insert info about one client and
> > > > its default address. Here is how:
>
> > > > 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)
> > > >     ifform.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 theform'
> > > >     return dict(form=form)
>
> > > > Notice the SQLFORM.factory (it makes ONEformusing public fields from
> > > > bothtablesand inherits their validators too).
> > > > Onformaccepts this does two inserts (some data in one table and some

mart

unread,
Jan 10, 2011, 4:24:03 AM1/10/11
to web2py-users
Hi, Just tried this and does the trick! :) I''m just a little unclear
on how these two (or more?) tables are linked once submitted?

Example: request forms.
There can be many types of requests and let's say that they all have a
set fields that all request forms have (like: name, email, priority,
created_on, etc...), then maybe the specifics can be unique to the
request (I.e. a new_build_request vs buildSystem_feature_request) -

In this case, I would combine the common 'rm_request' table & the
'new_build_request' table, then similarly, 'rm_request' table & the
'buildSystem_feature_request'

So when the form gets submitted, how does the select() work? How do we
correlate the data from rm_request to the new_build_request data?


And ditto on Carlos' question about updates :) How would that work?

Thanks,
Mart :)



On Dec 12 2010, 4:33 pm, Carlos <carlosgali...@gmail.com> wrote:
> Hi,
>
> I am wondering how this solution (singleformwithlinkedtables) can

Arun K.Rajeevan

unread,
Jan 10, 2011, 7:45:32 AM1/10/11
to web...@googlegroups.com
This one was there at web2pyslice's

But It doesn't worked for me when I had a field of type file in one of the forms.
Yes, it worked fine on rocket server comes with web2py.
But I had to make that work on GAE (but it doesn't worked)

I had a long thread here. And waiting Massimo to figure out what went wrong. 

mart

unread,
Jan 10, 2011, 12:33:12 PM1/10/11
to web2py-users
Yes, I saw the slice and was all sounds great, I just dont have the
big picture on how they stay linked (at least i don't see it)..other
thing that may do the trick is a variation on the "audit slice"...
looking into that one....

Do we know if this 'Tip of the day' works well with the audit (saw
that also on web2pyslice)... by putting it in one the tables, will
that be linked to the second table as well?

Thanks,
Mart :)
Reply all
Reply to author
Forward
0 new messages