Skip to content

Commit

Permalink
[IMP] Improve domain using Many2many fields
Browse files Browse the repository at this point in the history
  • Loading branch information
unaiberis committed Dec 9, 2024
1 parent 8445646 commit 38b1715
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 29 deletions.
39 changes: 25 additions & 14 deletions survey_user_input_domain/models/survey_user_input_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,42 @@
class SurveyUserInputLine(models.Model):
_inherit = "survey.user_input.line"

calculated_matrix_question_id = fields.Many2one(
comodel_name="survey.question",
string="Matrix Question ID",
calculated_matrix_question_ids = fields.Many2many(
comodel_name="survey.question.answer",
relation="survey_user_input_line_matrix_answer_rel",
column1="user_input_line_id",
column2="question_answer_id",
string="Matrix Question Answers",
compute="_compute_matrix_question_id",
store=True,
)
calculated_suggested_answer_question_id = fields.Many2one(
comodel_name="survey.question",
string="Suggested Answer ID",

calculated_suggested_answer_question_ids = fields.Many2many(
comodel_name="survey.question.answer",
relation="survey_user_input_line_suggested_answer_rel",
column1="user_input_line_id",
column2="question_answer_id",
string="Suggested Answer Questions",
compute="_compute_suggested_answer_id",
store=True,
)

@api.depends("matrix_row_id", "matrix_row_id.matrix_question_id")
def _compute_matrix_question_id(self):
for line in self:
line.calculated_matrix_question_id = (
line.matrix_row_id.matrix_question_id if line.matrix_row_id else False
)
if line.matrix_row_id and line.matrix_row_id.matrix_question_id:
line.calculated_matrix_question_ids = (
line.matrix_row_id.matrix_question_id.matrix_row_ids
)
else:
line.calculated_matrix_question_ids = False

@api.depends("suggested_answer_id", "suggested_answer_id.question_id")
def _compute_suggested_answer_id(self):
for line in self:
line.calculated_suggested_answer_question_id = (
line.suggested_answer_id.question_id
if line.suggested_answer_id
else False
)
if line.suggested_answer_id and line.suggested_answer_id.question_id:
line.calculated_suggested_answer_question_ids = (
line.suggested_answer_id.question_id.suggested_answer_ids
)
else:
line.calculated_suggested_answer_question_ids = False
45 changes: 30 additions & 15 deletions survey_user_input_domain/views/survey_user_input_form.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="survey_user_input_line_view_form" model="ir.ui.view">
<field name="model">survey.user_input.line</field>
<field name="inherit_id" ref="survey.survey_user_input_line_view_form" />
<field name="arch" type="xml">
<field name="suggested_answer_id" position="attributes">
<attribute
name="domain"
>"[('question_id', '=', record.suggested_answer_id.question_id)]"</attribute>
</field>
<data>
<record id="survey_user_input_line_view_form" model="ir.ui.view">
<field name="name">survey.user_input.line.view.form</field>
<field name="model">survey.user_input.line</field>
<field name="inherit_id" ref="survey.survey_user_input_line_view_form" />
<field name="arch" type="xml">
<field name="suggested_answer_id" position="before">
<field
name="calculated_suggested_answer_question_ids"
attrs="{'invisible': True}"
/>
</field>
<field name="matrix_row_id" position="before">
<field
name="calculated_matrix_question_ids"
attrs="{'invisible': True}"
/>
</field>

<field name="matrix_row_id" position="attributes">
<attribute
name="domain"
>"[('question_id', '=', record.matrix_row_id.question_id)]"</attribute>
<field name="suggested_answer_id" position="attributes">
<attribute
name="domain"
>[('id', '=', calculated_suggested_answer_question_ids)]</attribute>
</field>
<field name="matrix_row_id" position="attributes">
<attribute
name="domain"
>[('id', '=', calculated_matrix_question_ids)]</attribute>
</field>
</field>
</field>
</record>
</record>
</data>
</odoo>

0 comments on commit 38b1715

Please sign in to comment.