create and update in one form

65 views
Skip to first unread message

Carlos Hanson

unread,
Jul 24, 2014, 6:31:11 PM7/24/14
to web...@googlegroups.com
Greetings,

Using plugin_attachments as an example, it defines a table like the following:

db.define_table('plugin_attachments_attachment',
   
Field('tablename', readable=False, writable=False),
   
Field('record_id', 'integer', readable=False, writable=False),
   
[...])

That suggests that I need to create the record to which I want to add an attachment before I add the attachment. Since I want to create the record and add the attachment in one view, I do the following:

def add():
    article_id
= db.article.insert()
    redirect
(URL('edit', args=[article_id]))

def edit():
    article_id
= request.args[0]
    form
= crud.update(db.article, article_id, next='view/[id]')
    attachments
= PluginAttachments(db.article, article_id)
   
return locals()

Ideally, I would like one function for add and one for edit because the current method complicates the code in a new project. Maybe I need to figure out how to submit multiple forms at once, then insert the extras, like attachments, after checking form.accepted.

Part of the problem is that I am using CKEditor and it wants to upload images to the server before I ever hit Save on the article.

Is there a suggested way to create a record and provide an update form in one controller? Should I just create one big custom form (SQLFORM.factory), then manage the inserts myself?

Thanks in advance for your suggestions and experience. Let me know if you need more clarification.

Carlos Hanson


黄祥

unread,
Jul 24, 2014, 6:36:57 PM7/24/14
to web...@googlegroups.com
had you already try SQLFORM.grid??
ref:
http://web2py.com/books/default/chapter/29/07/forms-and-validators#SQLFORM-grid

best regards,
stifan

Anthony

unread,
Jul 24, 2014, 7:07:11 PM7/24/14
to web...@googlegroups.com

Carlos Hanson

unread,
Jul 24, 2014, 7:53:09 PM7/24/14
to
I have only skimmed SQLFORM.grid. My concern is for one record at a time rather than a grid of multiple records, but I will review it in more detail to see if I can apply it. Thanks.

Carlos

Carlos Hanson

unread,
Jul 24, 2014, 8:02:54 PM7/24/14
to
Multiple tables in one form is one of the solutions I was considering as I tried to formulate my question. I hadn't found that particular section, but I remembered reading something about dealing with multiple tables.

One issue that doesn't solve is an ajax call to add to a separate table. In the case of CKEditor, if I insert an image to my text, it uploads the image via ajax, so if I keep with the tablename/record_id, then I need to send the record_id of the article that CKEditor is working on. Without a pre-existing article, I cannot insert the image through CKEditor when creating a new article. I would have to save the article then edit it to start inserting images.

For my current project, I am taking out the option of adding images that way, so the SQLFORM.factory() option may be the way to go. I will need to change how my attachments are uploaded, since they currently submit via ajax too.

Thanks.
Reply all
Reply to author
Forward
0 new messages