My suggestion: First we change the 'question' table definition as follows
db.define_table('question', Field('survey', db.survey), Field('title', length=200), Field('created', 'datetime', default=request.now, writable=False), Field('question_type', 'string', default='text'), Field('choices', 'text', default='') ) After, in the view where is the form to build the question, we advice the user that the options (in the textarea) should be separated with commas if question type is in [radio, checkboxes, select]. So we will not parse the textarea content when the question is saved in database, but we will have to parse the question choices field in the controller that manages the survey. For example (warning: code not optimized and tested, it is only a proof of concept): def display_questions(): question_id = request.args(0) question_selected = db(db.question.id==question_id).select().first() question_type = question_selected.question_type question_choices = question_selected.choices question_title = question_selected.title if question_type in ['radio', 'checkboxes', 'select']: choices = [c.strip() for c in question_choices.split(',')] if question_type == 'radio': widget = SQLFORM.widgets.radio.widget elif question_type == 'checkboxes': widget = SQLFORM.widgets.checkboxes.widget elif question_type == 'select': widget = SQLFORM.widgets.options.widget else: widget = SQLFORM.widgets.string.widget form = SQLFORM.factory(Field('question', label=question_title, widget=widget, requires=IS_IN_SET(choices))) else: form = SQLFORM.factory(Field('question', label=question_title, widget=SQLFORM.widgets.text.widget, requires=IS_NOT_EMPTY())) if form.process().accepted: ... save the answer in database else: ... display error message ... return dict(form=form) I hope that this help you to find the right solution. Il giorno venerdì 10 gennaio 2014 14:42:55 UTC+1, Timo Bahner ha scritto: > > I'm trying to do something like this: > > TEXT = 'text' > RADIO = 'radio' > SELECT = 'select' > > QUESTION_TYPES = ( > (TEXT, 'text'), > (RADIO, 'radio'), > (SELECT, 'checkboxes'),) > > def get_choice(): > choices = choices.split(',') > choices_list = [] > for c in choices: > c = c.strip() > choices_list.append((c,c)) > choices_tuple = tuple(choices_list) > return choices_tuple > > db.define_table('survey', > Field('name', unique=True), > Field('created', 'datetime', default=request.now, writable=False), > Field('is_active', 'boolean', default=False)) > > db.survey.name.requires = IS_NOT_IN_DB(db, db.survey.name) > > db.define_table('question', > Field('survey', db.survey), > Field('title', length=200), > Field('created', 'datetime', default=request.now, writable=False), > Field('question_type', default='text', widget=SQLFORM.widgets.radio. > widget), > Field('choices', 'text', default=get_choice()) > ) > > db.question.survey.requires = IS_IN_DB(db, db.survey.id) > db.question.question_type.requires = IS_IN_SET(QUESTION_TYPES)Enter code > here... > > Now I need somehow to call get_choice() from db.question.choice to split > radio and checkbox answers. > > Again: what I'm trying to accomplish is to set up a survey. I want to > create a set of questions with appadmin which can include radio, checkboxes > or text answers. > > P.S. I'm fairly new to python :) > > P.P.S. Or maybe I can leave it like this (but remove get_choice() from > db.py), split radio and checkbox answers with a comma and creat a split > function in controller. How's that? Complicated? > -- 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 web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.