计算字段 Odoo9

Computed fields Odoo9

我正在 "Odoo9" 中创建一个学生管理模块,在该模块的一部分中,我想计算学生在 "maths" 这样的科目中获得的平均分数。我'我试图使用这段代码来实现,但是我在填充 "Maths-1" 和 "Maths-2" 后立即计算 "Avg-Maths" 时遇到问题,它只能在保存学生 profile.Can 某人后计算请帮助我在这里意识到这个问题?我该如何解决这个问题?

#student class
class student_student(models.Model):
    '
    '
    '
    result_ids = fields.One2many("schoolresults.detail", "student_id", "School Results")
    '
    '
    '


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_manual = fields.Float("Result")
    result = fields.Float(compute='_compute_value',store=True)
    manual = fields.Boolean(compute='_is_manual', default=False)

    @api.one
    @api.depends('manual')
    def _is_manual(self):
        self.manual = self.subject_id.my_id
    @api.one
    @api.depends('result_manual','subject_id','subject_id.my_id')
    def _compute_value(self):
        self.ensure_one()
        results = self.env['schoolresults.detail'].search([])
        total = 0
        for data in results:
            total += data.result_manual
        for data in results:
            #if the subject is the average of others 
            if data.subject_id.my_id:
                data.result = total


class schoolresults_subject(models.Model):
    _name = "schoolresults.subject"
    _description = "Student's subjects."
    my_id = fields.Integer(default=0)
    name = fields.Char("Subject")

student_id.result_ids.result_manual 添加到 _compute_value 上的依赖列表。那应该触发重新计算。

我认为在计算完值后,您应该将其分配给结果字段

@api.one
def _compute_method(self):
# compute average for this record 
self.result = calclated_value

但我没有看到您将值分配给结果字段?!!所以尝试分配它应该做的