Formset or subforms

343 views
Skip to first unread message

Vinicius Assef

unread,
Apr 23, 2013, 9:23:46 AM4/23/13
to web2py-d...@googlegroups.com

Vinicius Assef

unread,
May 3, 2013, 10:39:48 AM5/3/13
to web2py-d...@googlegroups.com
Anybody there with this subject?

Alan Etkin

unread,
May 3, 2013, 12:55:07 PM5/3/13
to web2py-d...@googlegroups.com
Hi guys,
I saw these 2 threads:

On the second post

> I found some pieces of code here and there that enabled me
> to come close to doing it using web2py but I consider my efforts a
> hack (and an incomplete one so far :-))

A hack is anything one has to code because it wasn't found in the core? I don't think so

The problem I think is that there's no conventional way (one and only one) of doing one-to-many record crud. For example, It wouldn't be too complex to implement a single page updating a table and retrieving and updating insert/update form components upon certain user events, such as clicking records. The tools needed are, I think, jQuery, web2py_component, SQLTABLE and SQLFORM. In fact, I implemented a similar tool for electronic invoice. The code is hosted here: http://code.google.com/p/pyafipws/source/checkout?repo=web2py-app

Things are even simpler using SQLFORM.grid, provided that you separate the form main data from the child records actions.

Maybe we can add a recipe to web2pyslices for this case, so users do not start from scratch when they have a similar requirement (unless someone has already shared such tool).

Niphlod

unread,
May 3, 2013, 2:45:36 PM5/3/13
to web2py-d...@googlegroups.com
yep, this kind of thing is easily accomplishable with components.
A more tight control on what happens with javascript would be needed to make the code less verbose, but is doable nonetheless with the current toolset in web2py.
I'm working in the tighter integration but it may take some time (see the post "no more inline javascript").

Let's take invoices as an example (usually "normalized" in "header" and "details" fields)
There are many ways to accomplish such a thing from the "architectural" standpoint....
You can generate on-the-fly (factory) a single huge form with fields like
detail_1_field1
detail_1_field2
....
detail_2_field1
detail_2_field2

and do your own dbio, all in one transaction.
An acceptable one can be very well be a "please fill the invoice header", save that one in the headers table and then prepare some "details" forms (one for each row of the "details" table).
There would be a "add detail" button that prepares another insert form for the subsequent detail and a "confirm this invoice" button that prevents any other addition and "freezes" the invoice as it is from further edits, e.g. turning a flag on the "headers" table (so you can distinguish in your code between "drafts" and "real" invoices).

The first method can be more correct from the "acid" standpoint but the second can, e.g. accumulate statistics on what users do in their drafts ..... it's all a matter of what your app needs to do.

Richard Vézina

unread,
May 3, 2013, 2:57:55 PM5/3/13
to web2py-d...@googlegroups.com
I recently found this solution :


I should had talk about nb_rows instead of nb_inputs...

But the solution works well...

Not sure it could solve your use case though.

I can provide more complet example, because I improve my code a bit...

The only reaming issue I have is that if there is input that can't be NULL and the user has select a greater number of rows then he really need the form will never submit... I think this could be solve by ajax submit of each input as I think Anthony suggest at the end of the thread (I didn't test)... But it by pass the web2py validator this way...

Hope it helps.

Richard


--
-- mail from:GoogleGroups "web2py-developers" mailing list
make speech: web2py-d...@googlegroups.com
unsubscribe: web2py-develop...@googlegroups.com
details : http://groups.google.com/group/web2py-developers
the project: http://code.google.com/p/web2py/
official : http://www.web2py.com/
---
You received this message because you are subscribed to the Google Groups "web2py-developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py-develop...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Alan Etkin

unread,
May 5, 2013, 4:24:57 PM5/5/13
to web2py-d...@googlegroups.com
El viernes, 3 de mayo de 2013 15:45:36 UTC-3, Niphlod escribió:
yep, this kind of thing is easily accomplishable with components.
A more tight control on what happens with javascript would be needed to make the code less verbose, but is doable nonetheless with the current toolset in web2py.

I have implemented an appliance using something like the second Niphlod approach here: http://code.google.com/p/invoice2py. Maybe this is a better starting point for new users wanting to create apps with linked forms.

However, once web2py js gets refactored, it would be a nice enhancement to add an ajax form for multiple linked records. Something like the following

>>> SQLFORM.multi(parent_table, child_table)

To allow creating the main record and do crud for the child table with background ajax api calls, all with one helper instance.

Regards
Reply all
Reply to author
Forward
0 new messages