如何在Odoo 11中将数据从一个表单传递到另一个表单?

huangapple go评论67阅读模式
英文:

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

问题

我有三个模型。

class student(models.Model):

    _name = 'student.info'
    _description = '学生信息'

    name = fields.Char('姓名', required=True)
    age = fields.Integer('年龄', compute='_compute_age', store=True)
    date_of_birth = fields.Date('出生日期', required=True)
    teacher_id = fields.Many2one('teacher.info', '老师')

class teacher(models.Model):

    _name = 'teacher.info'
    _description = '老师信息'

    name = fields.Char('姓名', required=True)
    position = fields.Char('职位')
    section_id = fields.Many2one('stu.section', '班级')
    student_ids = fields.Many2many('student.info', 'student_teacher_rel', 'teacher_id', 'student_id', '学生')

class section(models.Model):

    _name = 'stu.section'
    _description = '学生班级'

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

两个动作:

<record id="action_student_to_class" model="ir.actions.act_window">
    <field name="name">班级</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">班级</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.ids,
          'default_section_id': active_id.id,
   }</field>
</record>

两个按钮。一个在学生表单视图中,另一个在班级表单视图中。

<button name="%(action_student_to_class)d"
        type="action"
        string="加入班级"
        class="oe_highlight"/>
    
<button name="%(action_class_to_teacher)d"
        type="action"
        string="分配老师"
        class="oe_highlight"/>

当从学生表单点击"加入班级"按钮时,该动作会将当前学生添加到班级的学生列表中。但是,当从班级表单点击"分配老师"按钮时,我希望同时更新教师表单中的当前班级和班级的学生列表。我已经通过从Python返回动作来实现了这一点,但我想从XML中实现。如何做到这一点?谢谢。

英文:

I have three models.

class student(models.Model):

    _name = &#39;student.info&#39;
    _description = &#39;Students\&#39; Information&#39;

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

class teacher(models.Model):

    _name = &#39;teacher.info&#39;
    _description = &#39;Teachers\&#39; Information&#39;

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

class section(models.Model):

    _name = &#39;stu.section&#39;
    _description = &#39;Students\&#39; Sections&#39;

    name = fields.Char(&#39;Name&#39;, required=True)
    student_ids = fields.Many2many(&#39;student.info&#39;, &#39;student_section_rel&#39;, &#39;section_id&#39;, &#39;student_id&#39;)

two actions,

        &lt;record id=&quot;action_student_to_class&quot; model=&quot;ir.actions.act_window&quot;&gt;
            &lt;field name=&quot;name&quot;&gt;Class&lt;/field&gt;
            &lt;field name=&quot;res_model&quot;&gt;stu.section&lt;/field&gt;
            &lt;field name=&quot;view_type&quot;&gt;form&lt;/field&gt;
            &lt;field name=&quot;view_mode&quot;&gt;form&lt;/field&gt;
            &lt;field name=&quot;context&quot;&gt;{&#39;default_student_ids&#39; : [(4, active_id)]}&lt;/field&gt;
        &lt;/record&gt;

        &lt;record id=&quot;action_class_to_teacher&quot; model=&quot;ir.actions.act_window&quot;&gt;
            &lt;field name=&quot;name&quot;&gt;Class&lt;/field&gt;
            &lt;field name=&quot;res_model&quot;&gt;teacher.info&lt;/field&gt;
            &lt;field name=&quot;view_type&quot;&gt;form&lt;/field&gt;
            &lt;field name=&quot;view_mode&quot;&gt;form&lt;/field&gt;
            &lt;field name=&quot;context&quot;&gt;{
                  &#39;default_student_ids&#39; : active_id.student_ids,
                  &#39;default_section_id&#39; : active_id,
           }&lt;/field&gt;
        &lt;/record&gt;

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

&lt;button name=&quot;%(action_student_to_class)d&quot;
        type=&quot;action&quot;
        string=&quot;To Class&quot;
        class=&quot;oe_highlight&quot;/&gt;

&lt;button name=&quot;%(action_student_to_class)d&quot;
        type=&quot;action&quot;
        string=&quot;To Class&quot;
        class=&quot;oe_highlight&quot;/&gt;

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.

答案1

得分: 0

我已找到解决方案。

如果在按钮的操作中不添加上下文也没问题。我们可以在按钮中添加上下文,例如:

<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}"/>
英文:

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

&lt;button name=&quot;%(action_student_to_class)d&quot;
        type=&quot;action&quot;
        string=&quot;To Class&quot;
        class=&quot;oe_highlight&quot;
        context=&quot;{&#39;default_student_ids&#39; : student_ids, &#39;default_section_id&#39; : active_id}&quot;/&gt;

huangapple
  • 本文由 发表于 2020年1月3日 13:17:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59573525.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定