custom update form multiple tables and list:string

29 views
Skip to first unread message

Yoel Benitez Fonseca

unread,
Aug 29, 2016, 2:24:19 PM8/29/16
to web...@googlegroups.com
h!

i got 2 tables, i need to make one update form for both, let's say they are:

---------
db.define_table("item",
   
...
   
Field('keywords', 'list:string', ...)
   
...
)
db
.define_table('concrete_item',
   
Field('item_id', 'reference item'),
   
...
)


---------
In the controller:
---------
def edit():
    item
= db.item(request.args(0))
    concrete_item
= db.concrete_item(item_id=item.id)
    fields
= []
   
for f in item:
        fields
.append(f)
   
for f in concrete_item:
        fields
.append(f)

    form
= SQLFORM.factory(*fields)
   
# put values to form fields
   
for f in db.item:
       form
.vas[f.name] = item[f.name]
   
for f in db.concrete_item:
       form
.vas[f.name] = concrete_item[f.name]
   
   
if form.proccess().accepted:
       
# here call update_record, etc.
       
pass

   
return dict(form=form)


---------

The problem is with the keywords field, when the form is rendered in the view it don't show the current value for the field. Meanwhile if i just make the update form for only one table:

--------
def edit()
    item
= db.item(request.args(0))

    form
= SQLFORM.factory(db.item, record=item)
   
return dict(form=form)


--------

The keywords field show the current values. Can any1 help me with this ?

--
Yoel Benítez Fonseca
http://redevil.cubava.cu/
$ python -c "import this"

Yoel Benitez Fonseca

unread,
Aug 30, 2016, 10:24:02 AM8/30/16
to web2py-users
Solved in some extend:

The problem is that each widget is initialized with the record values on the SQLFORM constructor if any, in the case you need to pre-populate the form (http://web2py.com/books/default/chapter/29/07/forms-and-validators#Pre-populating-the-form) it work flawless for all the other fields except for 'list:string', in my case the 'keywords' field.

I'm getting around the problem using defaults, in the controller:

def edit():
    item
= db.item(request.args(0))
    concrete_item
= db.concrete_item(item_id=item.id)
    fields
= []

    # before adding the the fields
    db.item.keywords.default = item.keywords
    # ---
   
for f in item:

        fields
.append(f)
   
for f in concrete_item:
        fields
.append(f)

    form
= SQLFORM.factory(*fields)
   
# put values to form fields
   
for f in db.item:
       form
.vas[f.name] = item[f.name]
   
for f in db.concrete_item:
       form
.vas[f.name] = concrete_item[f.name]
   
   
if form.proccess().accepted:
       
# here call update_record, etc.
       
pass

   
return dict(form=form)
Reply all
Reply to author
Forward
0 new messages