2015-08-06 1 views
1

Я хочу к получить сумму суммарного поля ru.invoice, который будет отображаться на ru.students, если имя поля ru.students равно student_id из ru.invoice.Получить сумму другой базовой модели поля при условии

Я использовал метод 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") 
+0

Можете ли вы уточнить, что «это не сработало»? Вы получили исключение? Получили ли вы другой результат, чем ожидалось? См. Http://stackoverflow.com/help/how-to-ask –

ответ

1

Прежде всего, будьте осторожны, не следует смешивать код API7 с API8 кода используйте всегда API8 код, если вы можете (опять же, это будет намного проще для вас использовать API8). Я думаю, что вы хотите это на поле payed (проверьте класс ru_invoice тоже, потому что я исправил некоторые вещи там - пример: в total поле вы написали _get_totals в compute, когда вы хотите позвонить _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') 
+0

Спасибо, что я пытался вычислить его, не добавляя одно2many поля. –

+0

Каждый раз, когда вы создаете поле «Many2one», вы должны создать поле «One2many» в инверсной таблице (хотя иногда это и не нужно). Я рад, что смог помочь вам! – forvas

+0

должно быть get_total или _get_total? – pourjour

 Смежные вопросы

  • Нет связанных вопросов^_^