Odoo继承 - 添加字段

时间:2017-04-06 15:12:03

标签: python-2.7 openerp odoo-9

我正在测试Odoo 9并试图弄清楚继承。使用以下代码,应将字段jason_test2,total_amount_invoice和total_balance_invoice添加到res.partner表中。它没有添加它们,我不知道它是否应该。

from openerp.osv import osv, fields
from openerp.tools.translate import _
from openerp import fields, models, api, exceptions, _
class ResPartner(models.Model):


_inherit = 'res.partner'
def _get_total_amount_invoice(self):
    res = {}
    for partner in self:
        invoice_obj = self.env['account.invoice']
        invoice_ids = invoice_obj.search([('partner_id', '=', partner.id)])
        amount_total = 0
        for invoice in invoice_ids:
            amount_total += invoice.amount_total
        res.update({'total_amount_invoice': amount_total})


def _get_total_balance_invoice(self):
    res = {}
    for partner in self:
        invoice_obj = self.env['account.invoice']
        invoice_ids = invoice_obj.search([('partner_id', '=', partner.id)])
        balance_total = 0
        for invoice in invoice_ids:
            balance_total += invoice.residual
        res.update({'total_balance_invoice': balance_total})


jason_test2 = fields.Char("Jason2")

total_amount_invoice = fields.Monetary(string='Amount Total', type='float',
                                       readonly=True, compute='_get_total_amount_invoice')
total_balance_invoice = fields.Monetary(string='Balance Total', type='float',
                                        readonly=True, compute='_get_total_balance_invoice')

2 个答案:

答案 0 :(得分:0)

除非是格式化问题,否则类定义的主体应缩进,否则它根本不属于类。

class ResPartner(models.Model):

    _inherit = 'res.partner'
    def _get_total_amount_invoice(self):
        res = {}
        for partner in self:
            invoice_obj = self.env['account.invoice']
            invoice_ids = invoice_obj.search([('partner_id', '=', partner.id)])
            amount_total = 0
            for invoice in invoice_ids:
                amount_total += invoice.amount_total
            res.update({'total_amount_invoice': amount_total})


    def _get_total_balance_invoice(self):
        res = {}
        for partner in self:
            invoice_obj = self.env['account.invoice']
            invoice_ids = invoice_obj.search([('partner_id', '=', partner.id)])
            balance_total = 0
            for invoice in invoice_ids:
                balance_total += invoice.residual
            res.update({'total_balance_invoice': balance_total})


    jason_test2 = fields.Char("Jason2")

    total_amount_invoice = fields.Monetary(string='Amount Total', type='float',
                                           readonly=True, compute='_get_total_amount_invoice')
    total_balance_invoice = fields.Monetary(string='Balance Total', type='float',
                                            readonly=True, compute='_get_total_balance_invoice')

答案 1 :(得分:0)

  

货币领域需要一个互补的货币领域来存储   金额的货币。

1添加一个字段以存储正在使用的货币

class LibraryBook(models.Model):
     # ...
     currency_id = fields.Many2one('res.currency', string='Currency')

2将货币字段添加到存储金额

total_amount_invoice = fields.Monetary(string='Amount Total', type='float',
                                   readonly=True, currency_field='currency_id' compute='_get_total_amount_invoice')