I have a default value for a field defined on the table definition,
along with its validators.
I want to create a form with a different initial value for some
fields, based on the link the user clicked. Something like "add new
item for this category" and the new item form come with that category
pre-filled.
The way I found to do this was:
form = SQLFORM(db.mytable)
form.custom.widget['myfield']['value'] = request.args(0)
form.custom.widget['myfield']._postprocessing()
the _postprocessing() call was necessary in case of multiple options
widget (like Selects), else the selected option wouldn't be updated.
It works fine, but I believe I am messing with the inners of the
object, a bad programming practice, as the internal implementation may
change and break my app.
Is there a proper way of doing this?
Kind regards,
Fabiano.
Before you create the FORM object you can do:
db.table.field.default = 0 (you temporarly change what you have defined
i models)
or after you have created the FORM but before if form.accepts
form.vars.field = something
Kenneth
After you have created the form but before if form.accepts define your
default.
form = SQLFORM(db.......)
form.vars.type = 'cleaner'
if form.accepts......
This should work.
Kenneth
db.table_user.email.writable=False # gives error
Exception: Target receiver address not specified
at the line of form=auth.register()
File "/home/ubuntu/web2py/gluon/tools.py", line 1683, in register
user = self.db(table_user[username] == form.vars[username]).select().first()
KeyError: 'email'
Maybe making table_user.email.writable=False
prevents auth.register() from creating a new table entry for the new user???
Again, for reference, the (essential) code:
def user():
if request.args(0)=='register':
registrant = db( db.registrant.token == request.vars.token ).select().first()
auth.settings.registration_requires_verification = False
db[auth.settings.table_user_name].email.writable=False
db[auth.settings.table_user_name].email.default=registrant.email
# this causes same error
# auth.settings.table_user.email.default = registrant.email
# auth.settings.table_user.email.writable=False
File "/home/ubuntu/web2py/gluon/tools.py", line 1683, in register
user = self.db(table_user[username] == form.vars[username]).select().first()
KeyError: 'email'
Yes, when I click on the invite and go to the website I get the registration form filled out with email (and name (excluded from my shortened example)) visible and non-editable.
Luis.
—«sent by mobile»—
File "/home/pierluigi/web2py/gluon/tools.py", line 1796, in register
user = self.db(table_user[username] == form.vars[username]).select().first()
KeyError: 'email'