Re: [web2py] one form for multiple tables - updating record

2011-07-11 Thread Kenneth Lundström


def display():
 record = db.person(request.args(0))
 form=SQLFORM.factory(db.person, db.affiliation, db.address, 
db.card, record)

 if form.accepts(request.vars, session):
 record.update_record(**dict(form.vars))
 return dict(form=form)

The problem is that your are giving SQLFORM a row object as you should 
give it an ID.

record = db.person(request.args(0)) creates a row object so instead o:
 form=SQLFORM.factory(db.person, db.affiliation, db.address, 
db.card, record)

try:
 form=SQLFORM.factory(db.person, db.affiliation, db.address, 
db.card, record[0].id)


or maybe even add an small check that request.args is correct:
 record = db.person(request.args(0))
 if len(record):
 form=SQLFORM.factory(db.person, db.affiliation, db.address, 
db.card, record)

 else:
 response.flash=(T('No record with that ID found'))


Kenneth




Traceback (most recent call last):
   File "/home/rwn/Projects/web2py/gluon/restricted.py", line192, in restricted
 exec ccode in environment
   File"/home/  
rwn/Projects/web2py/applications/g_bender/controllers/default.py"
  , line132, 
in
   File "/home/rwn/Projects/web2py/gluon/globals.py", line137, in
 self._caller = lambda f: f()
   File"/home/  
rwn/Projects/web2py/applications/g_bender/controllers/default.py"
  , line66, in update
 form=SQLFORM.factory(db.person, db.affiliation, db.address, db.card, 
record=record)
   File "/home/rwn/Projects/web2py/gluon/sqlhtml.py", line1226, in factory
 return SQLFORM(DAL(None).define_table(table_name, *fields), **attributes)
   File "/home/rwn/Projects/web2py/gluon/sqlhtml.py", line772, in __init__
 default = record[fieldname]
   File "/home/rwn/Projects/web2py/gluon/dal.py", line3701, in __getitem__
 return dict.__getitem__(self, key)
KeyError: 'organization'

I think the error is because  I'm only passing a record from db.person.

So how do I actually retrieve the same record set I entered using the 
technique presented in "one form for multiple tables"? I couldn't find 
a relevant example to follow in the book.


Thanks.








[web2py] one form for multiple tables - updating record

2011-07-11 Thread niknok
I've read the section about "one form for multiple tables"  and
following the example created my function that updates four tables: 

def register():
form=SQLFORM.factory(db.person, db.affiliation, db.address, db.card)
if form.accepts(request.vars):
id = db.person.insert(**db.person._filter_fields(form.vars))
form.vars.person=id
id = db.affiliation.insert(**db.affiliation._filter_fields(form.vars))
id = db.address.insert(**db.address._filter_fields(form.vars))
id = db.card.insert(**db.card._filter_fields(form.vars))
return dict(form=form)


That works fine. Now, following the book convention, I tried to display
the same record set but it produces an error:


def display():
record = db.person(request.args(0)) 
form=SQLFORM.factory(db.person, db.affiliation, db.address, db.card, record)

if form.accepts(request.vars, session):
record.update_record(**dict(form.vars))

return dict(form=form)



Traceback (most recent call last):
  File "/home/rwn/Projects/web2py/gluon/restricted.py", line 192, in restricted
exec ccode in environment
  File 
"/home/rwn/Projects/web2py/applications/g_bender/controllers/default.py", line 
132, in 
  File "/home/rwn/Projects/web2py/gluon/globals.py", line 137, in 
self._caller = lambda f: f()
  File 
"/home/rwn/Projects/web2py/applications/g_bender/controllers/default.py", line 
66, in update
form=SQLFORM.factory(db.person, db.affiliation, db.address, db.card, 
record=record)
  File "/home/rwn/Projects/web2py/gluon/sqlhtml.py", line 1226, in factory
return SQLFORM(DAL(None).define_table(table_name, *fields), **attributes)
  File "/home/rwn/Projects/web2py/gluon/sqlhtml.py", line 772, in __init__
default = record[fieldname]
  File "/home/rwn/Projects/web2py/gluon/dal.py", line 3701, in __getitem__
return dict.__getitem__(self, key)
KeyError: 'organization'


I think the error is because  I'm only passing a record from db.person. 

So how do I actually retrieve the same record set I entered using the
technique presented in "one form for multiple tables"? I couldn't find a
relevant example to follow in the book.

Thanks.