First off, I want to say thank you to the contributors of this group and of
the codebase, web2py is a godsend! Now to my problem.
Like the title implies, I'm getting two separate issues depending on how I
set up my controller logic. This first one produces an IntegrityError.
My controller:
@auth.requires_login()
def new():
response.menu = MENU
form = SQLFORM(db.text_messages, submit_button='Send!',
formstyle='bootstrap3_inline')
if form.validate():
session.flash = 'Congratulations!'
form.vars.phone_number_id = int(form.vars.phone_number_id)
payload = dict(**form.vars)
payload['user_id'] = auth.user.id
id = db.text_messages.insert(**payload)
redirect(URL('dashboard', 'index'))
elif form.errors:
response.flash = form.errors.number
return dict(form=form)
My models:
db.define_table('countries',
Field('name', writable=False),
Field('iso', writable=False, readable=False),
redefine=True)
db.define_table('numbers',
Field('country_id', db.countries),
Field('phone_number', 'string',
unique=True),
Field('user_id', db.auth_user,
#default=auth.user_id,
writable=False, readable=False),
redefine=True)
# does this validation need to exist if I make a reference to 'db.countries'?
db.numbers.country_id.requires = IS_IN_DB(db, 'countries.id', '%(name)s',
zero=T('choose one'),
error_message="Please select from the
list")
db.numbers.phone_number.requires = [IS_NOT_EMPTY(),
IS_NOT_IN_DB(db, 'numbers.phone_number')]
db.define_table('text_messages',
Field('text_message', 'string', requires=IS_NOT_EMPTY()),
Field('phone_number_id', db.numbers),
Field('recipients', 'list:string', requires=IS_NOT_EMPTY()),
Field('user_id', 'reference auth_user',
#default=auth.user.id,
writable=False, readable=False),
redefine=True)
db.text_messages.phone_number_id.requires = IS_IN_DB(db, 'numbers.id',
'%(phone_number)s',
zero=T('choose one'),
error_message="Please select from the
list")
And when the controller is like so:
@auth.requires_login()
def new():
response.menu = MENU
form = SQLFORM(db.text_messages, submit_button='Send!',
formstyle='bootstrap3_inline')
if form.process().accepted:
response.flash = 'form accepted'
elif form.errors:
response.flash = 'form has errors'
return dict(form=form)
... the form is inserted, but the text_messages.user_id is set to None. I'm
not quite sure what to do next (aside from blindly debugging and google
searching :P)
Any help is appreciated, and thanks in advance!
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.