根据条件得到其他模型字段的总和

时间:2015-08-06 21:06:41

标签: python python-2.7 odoo-8 odoo

如果ru.invoice的名称字段等于ru.students ru.students,我希望student_id的总字段总和显示在ru.invoice上1}}。

我使用browse方法,但它不起作用。

class ru_students(models.Model):
    _name = 'ru.students'
    _rec_name = 'code'

def _get_total(self, cr, uid, ids, context=None):
    pool_re = self.pool.get('ru.invoice')
    pool_rec = pool_re.browse(cr, uid, ids, [('name','=','student_id')],    
context=context)
    for field in self:
        for line in pool_rec:
            x = 0.0
            x += line.total
            field.payed += x

    name = fields.Char(string="Name")
    payed = fields.Float(compute="_get_total")


   class ru_invoice(models.Model):
       _name = 'ru.invoice'
       _rec_name = 'code'

    @api.multi
    @api.depends('qty','unit_price')
    def get_total(self):
        for rec in self:
            x = 0.0
            x = rec.qty * rec.unit_price
            rec.total = x


student_id = fields.Many2one('ru.students','Student ID")
qty = fields.Float(string="Quantity")
unit_price = fields.Float(string="Unit Price")
total = fields.Float(compute="_get_totals",string="Total")

1 个答案:

答案 0 :(得分:1)

首先,请注意不要将API7代码与API8代码混合使用,如果可以,请始终使用API​​8代码(此外,使用API​​8会更容易)。我认为您希望在自己的字段payed上查看此内容(请查看课程ru_invoice,因为我在那里更正了一些内容 - 示例:在total字段中您编写了_get_totalscompute时想要致电_get_total - )。

class ru_students(models.Model):
    _name = 'ru.students'
    _rec_name = 'code'

    @api.multi
    @api.depends('invoices')
    def _get_total(self):
        for student in self:
            student.payed = sum(
                invoice.total for invoice in student.invoices)

    name = fields.Char(string='Name')
    payed = fields.Float(compute='_get_total', string='Payed')
    invoices = fields.One2many(comodel_name='ru.invoice',
                               inverse_name='student_id',
                               string='Invoices of the student')


class ru_invoice(models.Model):
    _name = 'ru.invoice'
    _rec_name = 'code'

    @api.multi
    @api.depends('qty', 'unit_price')
    def _get_total(self):
        for invoice in self:
            invoice.total = invoice.qty * invoice.unit_price

    student_id = fields.Many2one(comodel_name='ru.students',
                                 string='Student ID')
    qty = fields.Float(string='Quantity')
    unit_price = fields.Float(string='Unit Price')
    total = fields.Float(compute='_get_total', string='Total')