计算字段Odoo9

时间:2016-08-10 17:28:40

标签: python-2.7 openerp odoo-9

我正在为“Odoo9”中的学生管理创建一个模块,在该模块的一部分中,我想要计算学生在“数学”这样的主题中获得的平均分数。我正在努力实现这一目标代码,但我在填写“数学-1”和“数学-2”后立即计算“平均数学”时遇到问题,只能在保存学生档案后计算出来。有人可以帮我解决这个问题吗?我该如何解决这个问题? enter image description here

#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")

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我认为在计算出值后你应该将它分配给你的结果字段

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

但是我没有看到你将值分配给结果字段?!! sot尝试分配它应该这样做