Reputation: 1
I would like to add a new question type to the survey: a Selection field(selection_option) appears in the front as a selection field type with its options. I have added a new question type to the model (survey.question) and store the options in the suggested_answer_ids everything is good in the back but when I am trying to test the survey and answer the question after selecting an option from the front and submit the answer, nothing get stored in the DB even thoug I have overrided the save_lines() and _save_line_choice in survey.user_input model. What elese should I do to get the answers stored In the back. Here is my code:
class SurveyQuestion(models.Model):
_inherit = "survey.question"
question_type = fields.Selection(
selection_add=[("selection_option", "Dropdown Menu")]
)
class SurveyUserInput(models.Model):
_inherit = "survey.user_input"
def save_lines(self, question, answer, comment=None):
""" Save answers to questions, depending on question type
If an answer already exists for question and user_input_id, it will be
overwritten (or deleted for 'choice' questions) (in order to maintain data consistency).
"""
old_answers = self.env['survey.user_input.line'].search([
('user_input_id', '=', self.id),
('question_id', '=', question.id)
])
if question.question_type in ['char_box', 'text_box', 'numerical_box', 'date', 'datetime']:
self._save_line_simple_answer(question, old_answers, answer)
if question.save_as_email and answer:
self.write({'email': answer})
if question.save_as_nickname and answer:
self.write({'nickname': answer})
elif question.question_type in ['simple_choice','selection_option', 'multiple_choice']:
self._save_line_choice(question, old_answers, answer, comment)
elif question.question_type == 'matrix':
self._save_line_matrix(question, old_answers, answer, comment)
else:
raise AttributeError(question.question_type + ": This type of question has no saving function")
def _save_line_choice(self, question, old_answers, answers, comment):
if not (isinstance(answers, list)):
answers = [answers]
if not answers:
# add a False answer to force saving a skipped line
# this will make this question correctly considered as skipped in statistics
answers = [False]
vals_list = []
if question.question_type == 'simple_choice':
if not question.comment_count_as_answer or not question.comments_allowed or not comment:
vals_list = [self._get_line_answer_values(question, answer, 'suggestion') for answer in answers]
if question.question_type == 'selection_option':
if not question.comment_count_as_answer or not question.comments_allowed or not comment:
vals_list = [self._get_line_answer_values(question, answer, 'suggestion') for answer in answers]
elif question.question_type == 'multiple_choice':
vals_list = [self._get_line_answer_values(question, answer, 'suggestion') for answer in answers]
if comment:
vals_list.append(self._get_line_comment_values(question, comment))
old_answers.sudo().unlink()
return self.env['survey.user_input.line'].create(vals_list)
the template: enter image description here
I have tried to test the survey, select an option and send the answers but the data of this selection field allways empty, I want it store my selection in the back.
Upvotes: 0
Views: 267
Reputation: 1314
Check that you have added the 'suggested_answer_ids' field to the survey.user_input.line model, this way:
suggested_answer_ids = fields.Many2many('survey.question.answer', string='Suggested Answers')
Modify the _save_line_choice method in the SurveyUserInput model this way:
def _save_line_choice(self, question, old_answers, answers, comment):
if not isinstance(answers, list):
answers = [answers]
if not answers:
answers = [False]
vals_list = []
if question.question_type == 'simple_choice' or question.question_type == 'selection_option':
if not question.comment_count_as_answer or not question.comments_allowed or not comment:
vals_list = [self._get_line_answer_values(question, answer, 'suggested_answer') for answer in answers]
elif question.question_type == 'multiple_choice':
vals_list = [self._get_line_answer_values(question, answer, 'suggested_answer') for answer in answers]
if comment:
vals_list.append(self._get_line_comment_values(question, comment))
old_answers.sudo().unlink()
return self.env['survey.user_input.line'].create(vals_list)
Upvotes: 0