为 One2many 字段设置默认值 [英] Set default values for One2many field
问题描述
我有一个 student_student
类,它有一个 one2many 字段result_ids
,定义如下:
I have a class student_student
which have a one2many fieldresult_ids
defined like the following:
result_ids = fields.One2many("schoolresults.detail", "student_id", "School Results", default="_get_subjects")
和
def _get_subjects(self):
cr = self.pool.cursor()
self.env
return self.pool.get('schoolresults.subject').search(cr, self.env.uid, [])
在另一边我有一个班级schoolresults_subject
:
in the other side I have a class schoolresults_subject
:
class schoolresults_subject(models.Model):
_name = "schoolresults.subject"
_description = "Student's subjects."
name = fields.Char("Subject")
class schoolresults_detail(models.Model):
_name = "schoolresults.detail"
_description = "Student's results."
student_id = fields.Many2one("student.student", "Student", ondelete="cascade")
subject_id = fields.Many2one("schoolresults.subject", "Subject")
result = fields.Float("Result", compute='_compute_value', store=True)
我想要做的是使用 one2many 中的 default
参数,每当用户尝试创建新的学生档案时,用上一堂课的科目列表填充 result_ids场地.但是每当我尝试创建学生档案时,我都会收到此错误Wrong values for student.student.result_ids
.请问有没有办法做到这一点?
What I'm trying to do is to fill the result_ids with a subjects list from the last class, whenever the user trying to create a new student profile, using the the default
parameter in the one2many field.
But whenever I try to create a student profile I get this error Wrong values for student.student.result_ids
.
Please is there anyway to achieve that?
附注.我正在使用 Odoo 9
推荐答案
我可以通过覆盖 default_get
方法来做到这一点:
I could do this by overriding the default_get
method:
def default_get(self, fields):
res = super(student_student, self).default_get(fields)
srd = self.env['schoolresults.detail']
ids=[]
school_result={'subject_id':1,'result':0} #dict for fields and their values
sr = srd.create(school_result)
ids.append(sr.id)
res['result_ids'] = ids
return res
这是如何覆盖 one2many 字段的 default_get
.
This is how to override default_get
for one2many field.
这篇关于为 One2many 字段设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!