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

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

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

问题

我有三个模型。

  1. class student(models.Model):
  2. _name = 'student.info'
  3. _description = '学生信息'
  4. name = fields.Char('姓名', required=True)
  5. age = fields.Integer('年龄', compute='_compute_age', store=True)
  6. date_of_birth = fields.Date('出生日期', required=True)
  7. teacher_id = fields.Many2one('teacher.info', '老师')
  8. class teacher(models.Model):
  9. _name = 'teacher.info'
  10. _description = '老师信息'
  11. name = fields.Char('姓名', required=True)
  12. position = fields.Char('职位')
  13. section_id = fields.Many2one('stu.section', '班级')
  14. student_ids = fields.Many2many('student.info', 'student_teacher_rel', 'teacher_id', 'student_id', '学生')
  15. class section(models.Model):
  16. _name = 'stu.section'
  17. _description = '学生班级'
  18. name = fields.Char('名称', required=True)
  19. student_ids = fields.Many2many('student.info', 'student_section_rel', 'section_id', 'student_id')

两个动作:

  1. <record id="action_student_to_class" model="ir.actions.act_window">
  2. <field name="name">班级</field>
  3. <field name="res_model">stu.section</field>
  4. <field name="view_type">form</field>
  5. <field name="view_mode">form</field>
  6. <field name="context">{'default_student_ids': [(4, active_id)]}</field>
  7. </record>
  8. <record id="action_class_to_teacher" model="ir.actions.act_window">
  9. <field name="name">班级</field>
  10. <field name="res_model">teacher.info</field>
  11. <field name="view_type">form</field>
  12. <field name="view_mode">form</field>
  13. <field name="context">{
  14. 'default_student_ids': active_id.student_ids.ids,
  15. 'default_section_id': active_id.id,
  16. }</field>
  17. </record>

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

  1. <button name="%(action_student_to_class)d"
  2. type="action"
  3. string="加入班级"
  4. class="oe_highlight"/>
  5. <button name="%(action_class_to_teacher)d"
  6. type="action"
  7. string="分配老师"
  8. class="oe_highlight"/>

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

英文:

I have three models.

  1. class student(models.Model):
  2. _name = &#39;student.info&#39;
  3. _description = &#39;Students\&#39; Information&#39;
  4. name = fields.Char(&#39;Name&#39;, required=True)
  5. age = fields.Integer(&#39;Age&#39;, compute=&#39;_compute_age&#39;, store=True)
  6. date_of_birth = fields.Date(&#39;Date of Birth&#39;, required=True)
  7. teacher_id = fields.Many2one(&#39;teacher.info&#39;, &#39;Teacher&#39;)
  8. class teacher(models.Model):
  9. _name = &#39;teacher.info&#39;
  10. _description = &#39;Teachers\&#39; Information&#39;
  11. name = fields.Char(&#39;Name&#39;, required=True)
  12. position = fields.Char(&#39;Position&#39;)
  13. section_id = fields.Many2one(&#39;stu.section&#39;, &#39;Section&#39;)
  14. student_ids = fields.Many2many(&#39;student.info&#39;, &#39;student_teacher_rel&#39;, &#39;teacher_id&#39;, &#39;student_id&#39;, &#39;Students&#39;)
  15. class section(models.Model):
  16. _name = &#39;stu.section&#39;
  17. _description = &#39;Students\&#39; Sections&#39;
  18. name = fields.Char(&#39;Name&#39;, required=True)
  19. student_ids = fields.Many2many(&#39;student.info&#39;, &#39;student_section_rel&#39;, &#39;section_id&#39;, &#39;student_id&#39;)

two actions,

  1. &lt;record id=&quot;action_student_to_class&quot; model=&quot;ir.actions.act_window&quot;&gt;
  2. &lt;field name=&quot;name&quot;&gt;Class&lt;/field&gt;
  3. &lt;field name=&quot;res_model&quot;&gt;stu.section&lt;/field&gt;
  4. &lt;field name=&quot;view_type&quot;&gt;form&lt;/field&gt;
  5. &lt;field name=&quot;view_mode&quot;&gt;form&lt;/field&gt;
  6. &lt;field name=&quot;context&quot;&gt;{&#39;default_student_ids&#39; : [(4, active_id)]}&lt;/field&gt;
  7. &lt;/record&gt;
  8. &lt;record id=&quot;action_class_to_teacher&quot; model=&quot;ir.actions.act_window&quot;&gt;
  9. &lt;field name=&quot;name&quot;&gt;Class&lt;/field&gt;
  10. &lt;field name=&quot;res_model&quot;&gt;teacher.info&lt;/field&gt;
  11. &lt;field name=&quot;view_type&quot;&gt;form&lt;/field&gt;
  12. &lt;field name=&quot;view_mode&quot;&gt;form&lt;/field&gt;
  13. &lt;field name=&quot;context&quot;&gt;{
  14. &#39;default_student_ids&#39; : active_id.student_ids,
  15. &#39;default_section_id&#39; : active_id,
  16. }&lt;/field&gt;
  17. &lt;/record&gt;

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

  1. &lt;button name=&quot;%(action_student_to_class)d&quot;
  2. type=&quot;action&quot;
  3. string=&quot;To Class&quot;
  4. class=&quot;oe_highlight&quot;/&gt;
  5. &lt;button name=&quot;%(action_student_to_class)d&quot;
  6. type=&quot;action&quot;
  7. string=&quot;To Class&quot;
  8. 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

我已找到解决方案。

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

  1. <button name="%(action_student_to_class)d"
  2. type="action"
  3. string="To Class"
  4. class="oe_highlight"
  5. 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

  1. &lt;button name=&quot;%(action_student_to_class)d&quot;
  2. type=&quot;action&quot;
  3. string=&quot;To Class&quot;
  4. class=&quot;oe_highlight&quot;
  5. 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:

确定