英文:
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 = '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.
答案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
<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}"/>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论