One form for two tables

39 views
Skip to first unread message

Andrea Marin

unread,
Apr 28, 2016, 6:04:01 PM4/28/16
to web2py-users
Hi all,

I'm trying to setup a form to manage two tables:

db.define_table('pratiche',
                Field('nome', requires=IS_NOT_EMPTY()),
                Field('descrizione', 'text', requires=IS_NOT_EMPTY()),
                Field('stato_pratica', requires=IS_IN_SET(['aperta', 'attesa', 'chiusa'])),
                auth.signature)

db.define_table('allegati',
                Field('tipo_allegato', requires=IS_IN_SET(['mandato', 'comparsa preliminare', 'relazione ctu', 'parcella'])),
                Field('doc_filename'),
                Field('doc', 'upload'),
                Field('pratica', 'reference pratiche', writable = False, readable = False))


Table allegati contain uploaded file referred to record in pratiche.
My controller form create a new record is:

@auth.requires_login()
def create():
    form = SQLFORM.factory(db.pratiche, db.allegati)
    form.vars.stato_pratica = 'aperta'
    form.vars.tipo_allegato = 'mandato'
    form.add_button('Back', URL('index'))
    if form.process(onvalidation=create_form_process).accepted:
        """TODO
           trovare il modo di non far inserire un nome di file
           ma di salvare almeno la categoria a cui si associa
           presa dal menu a tendina del tipo di file
        """
        id = db.pratiche.insert(**db.pratiche._filter_fields(form.vars))
        form.vars.pratiche=id
        id = db.allegati.insert(**db.allegati._filter_fields(form.vars))
        form.vars.allegati=id
        session.flash = T('Posted')
        redirect(URL('index'))
    elif form.errors:
        session.flash = T('Il form ha degli errori')
    return locals()

def create_form_process(form):
    form.vars.doc_filename = form.vars.tipo_allegato


And my views is

{{extend 'layout.html'}}

<h2>Nuova pratica</h2>
{{=form}}


The form is show well but when I try su submit I receive this error that I don't understand

Error ticket for "studiolegale"

Ticket ID

127.0.0.1.2016-04-28.23-56-24.d61a1296-63cc-4f78-9a35-ec021d430d86

<type 'exceptions.RuntimeError'> you must specify a Field(..., uploadfolder=...)

Versione

web2py™Version 2.14.5-stable+timestamp.2016.04.14.03.26.16
PythonPython 2.7.3: /Users/andrea/Desktop/web2py/web2py.app/Contents/MacOS/python (prefix: /Users/andrea/Desktop/web2py/web2py.app/Contents/Resources)

Traceback

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
Traceback (most recent call last):
File "/Users/andrea/Desktop/web2py/web2py.app/Contents/Resources/gluon/restricted.py", line 227, in restricted
File "/Users/andrea/Desktop/web2py/web2py.app/Contents/Resources/applications/studiolegale/controllers/default.py", line 95, in <module>
File "/Users/andrea/Desktop/web2py/web2py.app/Contents/Resources/gluon/globals.py", line 417, in <lambda>
File "/Users/andrea/Desktop/web2py/web2py.app/Contents/Resources/gluon/tools.py", line 4258, in f
File "/Users/andrea/Desktop/web2py/web2py.app/Contents/Resources/applications/studiolegale/controllers/default.py", line 13, in create
File "/Users/andrea/Desktop/web2py/web2py.app/Contents/Resources/gluon/html.py", line 2300, in process
File "/Users/andrea/Desktop/web2py/web2py.app/Contents/Resources/gluon/html.py", line 2238, in validate
File "/Users/andrea/Desktop/web2py/web2py.app/Contents/Resources/gluon/sqlhtml.py", line 1662, in accepts
File "/Users/andrea/Desktop/web2py/web2py.app/Contents/Resources/gluon/packages/dal/pydal/objects.py", line 1519, in store
RuntimeError: you must specify a Field(..., uploadfolder=...)

What are my errors?
I perform the right way to do this?

Thanks a lot

Anthony

unread,
Apr 28, 2016, 6:39:02 PM4/28/16
to web2py-users
When using SQLFORM.factory, a dummy DAL object is created and doesn't have any default upload folder, so for any upload fields, you must specify the "uploadfolder" argument.

Anthony

Anthony

unread,
Apr 28, 2016, 6:41:50 PM4/28/16
to web2py-users
Specifically, before creating the form, something like this should work:

    db.allegati.doc.uploadfolder = os.path.join(request.folder, 'uploads')

Anthony
Reply all
Reply to author
Forward
0 new messages