[英]Get sum of other model field base on condition
我想將得到的總場的總和ru.invoice
上顯示ru.students
如果name字段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")
首先,請注意不要將API7代碼與API8代碼混合使用,請盡可能使用API8代碼(此外,使用API8會容易得多)。 我認為您希望在您的字段上payed
(也請檢查ru_invoice
類,因為我在那里更正了一些內容- 例如:在total
字段中,當您想調用_get_total
時,您已經在compute
編寫了_get_totals
)。
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')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.