Search code examples
actionodooodoo-11

How to carry data from one form to another in odoo 11?


I have three models.

class student(models.Model):

    _name = 'student.info'
    _description = 'Students\' Information'

    name = fields.Char('Name', required=True)
    age = fields.Integer('Age', compute='_compute_age', store=True)
    date_of_birth = fields.Date('Date of Birth', required=True)
    teacher_id = fields.Many2one('teacher.info', 'Teacher')

class teacher(models.Model):

    _name = 'teacher.info'
    _description = 'Teachers\' Information'

    name = fields.Char('Name', required=True)
    position = fields.Char('Position')
    section_id = fields.Many2one('stu.section', 'Section')
    student_ids = fields.Many2many('student.info', 'student_teacher_rel', 'teacher_id', 'student_id', 'Students')

class section(models.Model):

    _name = 'stu.section'
    _description = 'Students\' Sections'

    name = fields.Char('Name', required=True)
    student_ids = fields.Many2many('student.info', 'student_section_rel', 'section_id', 'student_id')

two actions,

        <record id="action_student_to_class" model="ir.actions.act_window">
            <field name="name">Class</field>
            <field name="res_model">stu.section</field>
            <field name="view_type">form</field>
            <field name="view_mode">form</field>
            <field name="context">{'default_student_ids' : [(4, active_id)]}</field>
        </record>

        <record id="action_class_to_teacher" model="ir.actions.act_window">
            <field name="name">Class</field>
            <field name="res_model">teacher.info</field>
            <field name="view_type">form</field>
            <field name="view_mode">form</field>
            <field name="context">{
                  'default_student_ids' : active_id.student_ids,
                  'default_section_id' : active_id,
           }</field>
        </record>

and two buttons. One in student form view and the other in section form view.

<button name="%(action_student_to_class)d"
        type="action"
        string="To Class"
        class="oe_highlight"/>

<button name="%(action_student_to_class)d"
        type="action"
        string="To Class"
        class="oe_highlight"/>

When 'To Class' button is clicked from student form, the action updates the current student in the section student_ids. But when 'To Teacher' button is clicked from section form, I want to update both the current section and student_ids in section in the teacher form. I have done this by returning action from python, but I want to do this from xml? How can I achieve this? Thanks.


Solution

  • I have found the solution.

    It's okay if we don't add context in action for the button. We can add context in button like

    <button name="%(action_student_to_class)d"
            type="action"
            string="To Class"
            class="oe_highlight"
            context="{'default_student_ids' : student_ids, 'default_section_id' : active_id}"/>