def register(): form=SQLFORM.factory(db.client,db.address) if form.process().accepted: 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)
You just have to use _filter_fields() and you insert the image first, keep the id return by web2py then use it to populate your fk field image_reference... Above the example from the book : http://web2py.com/books/default/chapter/29/07#One-form-for-multiple-tables Richard On Mon, Mar 25, 2013 at 5:10 PM, BlueShadow <kevin.bet...@gmail.com> wrote: > Hi, > I got two tables which reference each other. to do that I added a third > called reference_image > db.define_table('Images', > Field('Title',length=1024), > Field('Image','upload'), > Field('thumb','upload',writable=False,readable=False), > Field('Source',db.Source, requires=IS_IN_DB(db,'Source.id', > 'Source.Name')), > format = '%(Title)s' > ) > > db.define_table('Media', > Field('Title',length=512), > Field('Content','text'), > Field('Views','integer',default=0), > Field('TitelImage169','reference Images', requires=IS_EMPTY_OR( > IS_IN_DB(db, db.Images.id,'Images.Bildunterschrift'), null=None)), > format = '%(Title)s' > ) > > > > db.define_table("image_references", > Field("image_id", "reference Images",writable=False,readable=False), > Field("Media", "reference Media") > ) > > when I enter an image I need to specify which media it belongs to. So I > did that with an SQLForm.factory(db.Image,db.image_reference) > my problem is I got no Idea how I fill the image_references.image_id. > Since the image Id has not been created at the time of the commit. > > my controller for the insert image is already pretty big because its > creating the thumbnails for the image. But here it is: > > def newImage(): > dbtable = db.Images #uploads table name > if len(request.args): > records = db(dbtable.id==request.args[0]).select() > if len(request.args) and len(records): > form = SQLFORM(dbtable, records[0], deletable=True) > else: > form = SQLFORM.factory(dbtable,db.image_references) > if form.accepts(request.vars, session): > response.flash = 'Entry for Images Database accepted,start > creating thumb' > makeThumbnail(dbtable,form.vars.id,(200,200)) > thisImage=db(dbtable.id==form.vars.id).select()[0] > elif form.errors: > response.flash = 'Error in Form for Images Database' > list = crud.select(dbtable) > return dict(form=form,list=list) > > > 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 web2py+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- --- 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.