The form factory would be the way to do this. You create a form which has 
both the parent information and the child information.

You'd add in your "if form.process().accepted:"
this:
id = db.parent.insert(**db.parent._filter_fields(form.vars))

that "id" is now the ID of the parent record, which you can use in your 
other inserts... like so...

form.vars.parent_id = id

then do your insert...

newid = db.child.insert(**db.child._filter_fields(form.vars))

and you can keep chaining all the way down if you want.


On Tuesday, March 12, 2013 5:47:45 PM UTC-7, Alex Glaros wrote:
>
> In example 34 below, is there a way to automatically get personID and 
> productID into the purchase table without needing to select them if the 
> person was already associating with the product?
>
> Let's say, Joe chooses to buy something from my store.  He clicks on the 
> product which takes him to smartgrid create-a-purchase record.  
>
> I think smartgrid makes him take an unnecessary step of choosing his name 
> from a list of registered users, then, goes to another unnecessary step to 
> make him select the product from the product database.
>
> Basically I'm asking that when creating a parent and detail record, how do 
> I automatically get the parent primary key into the child foreign key? Does 
> example 32 above with SQLFORM<http://www.web2py.com/init/global/vars/SQLFORM>
> .factory do what I'm seeking?
>
> (My real problem is slightly different and simpler:  User is creating a 
> parent record and will immediately create a child record at the same time. 
>  How can the parent PK get automatically inserted into/as the child 
> record's FK?)
>
> Thanks,
>
> Alex Glaros
>
> http://www.web2py.com/init/default/examples
> Example 34 *Using a Smartgrid*
>
> All of the above database examples can be condensed in one simple command 
> using the SQLFORM.smartgrid:
>
> 1.
> 2.
> 3.
> 4.
>
> def manage_transactions():
>     grid = SQLFORM 
> <http://www.web2py.com/init/global/vars/SQLFORM>.smartgrid(db.person,linked_tables=['product','purchase'],
>                              user_signature=False)
>     return dict(grid=grid)
>
>

-- 

--- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to