form / subform / adding row (field)...

977 views
Skip to first unread message

Richard Vézina

unread,
Jan 18, 2011, 7:00:50 PM1/18/11
to web...@googlegroups.com
Hello Massimo,

I am trying to insert data in 2 tables from one form... I would like to be able to insert any number of rows or records in the subtable.

I build this test app (see attach)... All the work I did is mostly in /default/register.html where there is jQuery script that allow adding new city input and delete new city input...


What should I do in register to allow the function to insert all the filled city clone field??

Next step will be adapted my script that it manage all the subform columns instead of only one.

Thanks.

Richard
web2py.app.testformfactory (1).w2p

Richard Vézina

unread,
Jan 18, 2011, 7:08:02 PM1/18/11
to web...@googlegroups.com
I forgot to paste :

def register():
    form=SQLFORM.factory(db.client, db.address, formstyle = 'divs') #, table_name='dummy_name')
    if form.accepts(request.vars):
        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)

What to do for what in red?

I think _filter_fields is the key of the problem...

I didn't have a look to it actually...

Richard

mart

unread,
Jan 19, 2011, 12:21:51 AM1/19/11
to web2py-users
I'm going to try this :) sorry, I mean to try your code from
yesterday. I think sub-forms would be an awesome addition. Anyways, if
it helps, I'll test and provide what i can :)

Mart

On Jan 18, 7:08 pm, Richard Vézina <ml.richard.vez...@gmail.com>
wrote:
> I forgot to paste :
>
> def register():
>     form=SQLFORM.factory(db.client, db.address, formstyle = 'divs') #,
> table_name='dummy_name')
>     if form.accepts(request.vars):
>         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)
>
> What to do for what in red?
>
> I think _filter_fields is the key of the problem...
>
> I didn't have a look to it actually...
>
> Richard
>
> On Tue, Jan 18, 2011 at 7:00 PM, Richard Vézina <ml.richard.vez...@gmail.com
>
>
>
>
>
>
>
> > wrote:
> > Hello Massimo,
>
> > I am trying to insert data in 2 tables from one form... I would like to be
> > able to insert any number of rows or records in the subtable.
>
> > I build this test app (see attach)... All the work I did is mostly in
> > /default/register.html where there is jQuery script that allow adding new
> > city input and delete new city input...
>
> > I built it from scratch for Web2py inspired by :
> >http://charlie.griefer.com/blog/index.cfm/2009/9/17/jQuery--Dynamical...

mart

unread,
Jan 19, 2011, 12:30:20 AM1/19/11
to web2py-users
Hey, es-tu de Montreal?

On Jan 18, 7:08 pm, Richard Vézina <ml.richard.vez...@gmail.com>
wrote:
> I forgot to paste :
>
> def register():
>     form=SQLFORM.factory(db.client, db.address, formstyle = 'divs') #,
> table_name='dummy_name')
>     if form.accepts(request.vars):
>         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)
>
> What to do for what in red?
>
> I think _filter_fields is the key of the problem...
>
> I didn't have a look to it actually...
>
> Richard
>
> On Tue, Jan 18, 2011 at 7:00 PM, Richard Vézina <ml.richard.vez...@gmail.com
>
>
>
>
>
>
>
> > wrote:
> > Hello Massimo,
>
> > I am trying to insert data in 2 tables from one form... I would like to be
> > able to insert any number of rows or records in the subtable.
>
> > I build this test app (see attach)... All the work I did is mostly in
> > /default/register.html where there is jQuery script that allow adding new
> > city input and delete new city input...
>
> > I built it from scratch for Web2py inspired by :
> >http://charlie.griefer.com/blog/index.cfm/2009/9/17/jQuery--Dynamical...

Richard Vézina

unread,
Jan 19, 2011, 9:12:55 AM1/19/11
to web...@googlegroups.com
;-) Originellement oui...

I would tell you on the orther thread that I post this one... I will do it just for the one that fall on the orther thread so they could keep track...

I continue today working on this.

Richard

mart

unread,
Jan 19, 2011, 9:48:14 AM1/19/11
to web2py-users
Excellent! je prends le train, direction MTL, a tous les quelques mois
pour me rendre au bureau (peut-etre un peu moins souvent l'hiver ;) )

SO, I have a question: Once, I use the button to add a field (which
does work well), what should be the expected back-end result? does one
of the tables get updated? or should a new table get generated based
on input type? How about a Field name? generic one name fits all (or
depends on type?)

Mart :)



On Jan 19, 9:12 am, Richard Vézina <ml.richard.vez...@gmail.com>
wrote:

Richard Vézina

unread,
Jan 19, 2011, 10:17:55 AM1/19/11
to web...@googlegroups.com
For now only adding and removing city input. I clone city input retag id='city' to id='city2' for the first clone and so on. I change all the id attribute of other tag cloned like divs. I had a garbage can icon and make it mouseover. That it for now.

Since I change the id of input to city2, city3 all those cloned input are not consider by the register controller for now. I have to figure out how _filter_fields works to modifying it (add regex hope it will work). Then I would like the controller to add as many rows in the "subform" or table2 (address table) as there is cloned city input fields.

If I am not changing the id of input it will not be w3c html compliant, but web2py will insert all the added input filled into city field of address table like this :

|city1|city2|city3|etc|

Not bad but not normalized schema and possibly problematic if length of city field is fixed or using to much space for nothing. 

If you feel that you know how to modify register function I will be glad for your help.

When it read, we could had a new recipe into the book ;-)

Richard

Richard Vézina

unread,
Jan 19, 2011, 10:21:59 AM1/19/11
to web...@googlegroups.com
If you move the script into the layout.html in the head it still works... You can then remove the view /view/default/register.html_

So you get access to admin, request, session, response button to see what going. You can see that city2 vars is store in session for example.

Richard

Richard Vézina

unread,
Jan 21, 2011, 3:34:16 PM1/21/11
to web...@googlegroups.com
Hello,

Here a version 0.00001 pre pre before alpha 
(see attachement)

But it works...

There is code in :

layout.html in head : js that I move there to get debug web2py button (no view)
default controller : register_new function and a couple of other functions

default/register_new

Add row button add all the rows you want. Fill it up submit... Go admin db.address see that there is a row for each added input for street and city.

Didn't test it to much.

I had to use request.vars to get street2, city2, etc. inputs that were filtered by .accept()

Validators not working for the added inputs... If one input is fill for required field it will pass and null will be entered in db.address for empty input.

Richard
330.gif
web2py.app.addingInputApp.w2p

Richard Vézina

unread,
Jan 24, 2011, 2:27:57 PM1/24/11
to web...@googlegroups.com
330.gif

skwasha

unread,
Feb 2, 2011, 6:45:22 PM2/2/11
to web2py-users
I'm curious if you ever got this all figured out? I wanna do something
similar (allow users to create lists of things of arbitrary list
length.) I'm new to web2py myself, so if you had something working
you'd be willing to share it'd be of great interest/help.

thanx

On Jan 24, 11:27 am, Richard Vézina <ml.richard.vez...@gmail.com>
wrote:
> Found this :
>
> http://code.google.com/p/django-dynamic-formset/
>
> <http://code.google.com/p/django-dynamic-formset/>Richard
>
> On Fri, Jan 21, 2011 at 3:34 PM, Richard Vézina <ml.richard.vez...@gmail.com
>
>
>
> > wrote:
> > Hello,
>
> > Here a version 0.00001 pre pre before alpha [?]
>  330.gif
> < 1KViewDownload

skwasha

unread,
Feb 2, 2011, 6:40:08 PM2/2/11
to web2py-users
I'm curious if you ever got this all figured out? I wanna do something
similar (essentially allow people to build up lists of things with
arbitrary list length.) If you had something complete I'd love to see
it if possible. Very new to web2py myself.

On Jan 24, 11:27 am, Richard Vézina <ml.richard.vez...@gmail.com>
wrote:
> Found this :
>
> http://code.google.com/p/django-dynamic-formset/
>
> <http://code.google.com/p/django-dynamic-formset/>Richard
>
> On Fri, Jan 21, 2011 at 3:34 PM, Richard Vézina <ml.richard.vez...@gmail.com
>
>
>
> > wrote:
> > Hello,
>
> > Here a version 0.00001 pre pre before alpha [?]
>  330.gif
> < 1KViewDownload

Massimo Di Pierro

unread,
Feb 2, 2011, 9:10:25 PM2/2/11
to web2py-users
I am not sure what the Django doce does. Did you look into
Field('items','list:string')?
Is that any close?

Richard Vézina

unread,
Feb 2, 2011, 10:34:44 PM2/2/11
to web...@googlegroups.com
The app I had attached in the preceding email works, but it broke the workflow. I can't get the added inputs values in form.vars, so I take them from request.vars or somewhere else don't remember... 

There is also an other problem with this hack... It not uses de validators for all the inputs... So as long as there is one filled input the form submit and the other added inputs will let pass empty value or if you prefer NULL will be inserted.

But at least you will be able to add all the field you want no matter how many field the subform contain... Remove the added field you no more want if you add to much inputs.

Submit and the data will be all linked or related to the id of the record of the "master" table or the base table form.

 The jQuery script has to be edited by hand for each subform (or subtable you use). If you modify your subtable (add a field) you will have to go change the jQuery script... I just tried to make a proof of concept and see what were the limits... So nothing fancy with the code. There is also a couple of jQuery lines that were not working properly in this version I think.

Ha yeah there is an other issue on read or update... There is no way to make it works with crud.read or crud.update (I did not try anything about that when I realize I broked the workflow).

Also, there is no way to make SQLFORM.factory works on update or read (edit your linked data)... At least there is no easy already available way. You have to crud.update on subtable and master table independantly to make any change after the first seizure.

I did not have time last week to continue to work on that.

I identified differents alternatives that I did not explored in details yet :

1) There is a "lib" in django for doing exactly that. I don't know django, but the basic (tutorial 1, 2, 3, 4).

2) There is also Pyjamas that could be use into web2py... It seems to me the most cumbersome alternative, since I have to learn many differents things.

3) WTForms... I also have to learn a lot to figure out if it could work with web2py or if it has to be stand alone. If it needs work separately I actualy think to explore if a loosely coupled framework could be more reliable for my needs (Flask, WTForms, WSGI, jinja...)

That where I am...

I appreciate any help to push further the reflexion or build something...

Richard

Richard Vézina

unread,
Feb 2, 2011, 10:57:12 PM2/2/11
to web...@googlegroups.com

Richard Vézina

unread,
Feb 4, 2011, 9:13:34 AM2/4/11
to web...@googlegroups.com
Here a other design I would implement...


Maybe not the MIME type, but to normalize the relation in :

Entity :
Master result
Repeated result

Relation
Master result one2many Repeated result

With this schema the repeated result will be normalize level 1 since you have to identify each record refer to wich time point for example...

So you get :

Attributes
Master result :
Date
Tech
Comment

Repeated result
result
time point
FK master result

Example many results in repeated result :

result1, tp0, FK1
result2, tp1, FK1
result3, tp2, FK1
result1, tp0, FK2
result2, tp1, FK2
result3, tp2, FK2

Normalize level 1 cause of TP


So I would do this :

Entity :
Master result
Repeated result
Time point

Relation
Master result one2many Repeated result
Repeated result one2many Time point

So now :

Repeated result 
result1, FK1, FKTP1
result2, FK1, FKTP2
result3, FK1, FKTP3
result1, FK2, FKTP1
result2, FK2, FKTP2
result3, FK2, FKTP3

Time point
id, tp
1, TP0
2, TP1
3, TP2

The form has to manage the insertion, update, read in 2 tables at a time since the time point records should be pretty much fixed.

I think the Android contact interface and the google Contact are pretty much good example of what I would achieve.

Richard
Reply all
Reply to author
Forward
0 new messages