Hello pbreit,

I cannot find the post on this group where I found it but I'm pretty
sure form.vars.filename_newfilename is what you're looking for.

This is not to be confused with request.vars.filename.filename which
will contain your original filename.  form.vars.filename_filename will
contain the new filename returned by store()

Rather than using two tables from models I am defining my Fields
directly in my SQLFORM.factory but that shouldn't matter in this case.

I have successfully uploaded files to the filesystem having
SQLFORM.factory calling store() and renaming the file for me.

I had to specify an uploadfolder similar to
uploadfolder=os.path.join(request.folder,'static/uploads/') in my
SQLFORM.factory.  (Just realized that's what you helped me with a few
days ago.)

Try this in your db.image.insert(...):

def newpost():
    form = SQLFORM.factory(db.post, db.image)
    if form.process().accepted:
        post_id = db.post.insert(**db.post._filter_fields(form.vars))
        db.image.insert(post_id=post_id,
            filename=form.vars.filename_newfilename)
        redirect(URL('post', args=post_id))
    return dict(form=form)


-David

On Oct 2, 4:23 am, pbreit <pbreitenb...@gmail.com> wrote:
> I can't seem to figure out how to do multiple tables in one form where an
> image upload is involved. I've tried various combinations of this.
> Unfortunately I'm not getting meaningful error messages right now,
> everything is listed as "timed out".
>
> db.define_table('post',
>     Field('title'),
>     Field('body', 'text'))
>
> db.define_table('image',
>     Field('post_id', db.post, readable=False, writable=False),
>     Field('filename', 'upload'))
>
> def newpost():
>     form = SQLFORM.factory(db.post, db.image)
>     if form.process().accepted:
>         post_id = db.post.insert(**db.post._filter_fields(form.vars))
>         db.image.insert(post_id=post_id,
>             filename=db.image.filename.store(form.vars.filename.file,
> form.vars.filename.filename))
>         redirect(URL('post', args=post_id))
>     return dict(form=form)

Reply via email to