def edit_demo():
response.view = 'producer/edit/edit_demo.load'
producer_id = request.get_vars.producer_id
producer = db(db.producer.id == producer_id).select().first()
form = None
if producer:
fields = ['name', 'address', 'city',
'state', 'zip_code', 'primary_contact',
'primary_phone', 'primary_email', 'secondary_contact',
'secondary_phone', 'secondary_email', 'grade',
'district']
extra_fields = []
for pt in db(db.producer_type.id > 0).select(orderby=db.producer_type.name):
if db((db.producer_producer_type.producer_type == pt.id) &
(db.producer_producer_type.producer == producer.id)).select().first():
has_producer_type = True
else:
has_producer_type = False
extra_fields.append(Field(fieldname='pt%s' % pt.id,
type='boolean',
default=has_producer_type,
label=pt.name))
form = SQLFORM(db.producer, record=producer_id, fields=fields,
showid=False, extra_fields=extra_fields,
table_name='edit_demographics', formname='edit_demographics_form')
if form.process().accepted:
response.flash = None
url = URL('index', args=['edit', 'producer', producer_id], user_signature=True, extension=False)
url += '&jump=demographics_jump'
redirect(url, client_side=True)
return dict(form=form, is_owner=is_owner, is_admin=is_admin)Traceback (most recent call last):
File "/home/jim/dev/web2py/gluon/restricted.py", line 219, in restricted
exec(ccode, environment)
File "/home/jim/dev/web2py/applications/connect/controllers/producer.py", line 1621, in <module>
File "/home/jim/dev/web2py/gluon/globals.py", line 422, in <lambda>
self._caller = lambda f: f()
File "/home/jim/dev/web2py/applications/connect/controllers/producer.py", line 1609, in edit_demo
table_name='edit_demographics', formname='edit_demographics_form')
File "/home/jim/dev/web2py/gluon/sqlhtml.py", line 1505, in __init__
default = record[fieldname]
File "/home/jim/dev/web2py/gluon/packages/dal/pydal/objects.py", line 145, in __getitem__
raise KeyError(key)
KeyError: 'pt3'
I'm not understanding how to define extra_fields in this context. In the end, the form is supposed to look like the attached. I'm trying to add the checkboxes at the bottom of the form.
I have it all working with SQLFORM.factory but would prefer that I do it with the base SQLFORM. Thoughts?
-Jim
if record:
default = record[fieldname]
else:
default = field.default
if isinstance(default, CALLABLETYPES):
default = default()if record and fieldname not in [x.name for x in extra_fields]:
default = record[fieldname]
else:
default = field.default
if isinstance(default, CALLABLETYPES):
default = default()