Python 激活时使用不同字段-奥多v8

Python 激活时使用不同字段-奥多v8,python,openerp,odoo-8,Python,Openerp,Odoo 8,假设我有以下字段: #Lamination packeting @api.onchange('cost_rs18', 'qty__m_18') def total18(self): if self.cost_rs18 or self.qty__m_18: self.total_18 = self.cost_rs18 * self.qty__m_18 cost_rs18 = fields.Float("Cost Rs.") qty__m_18 = fields.Integ

假设我有以下字段:

#Lamination packeting
@api.onchange('cost_rs18', 'qty__m_18')
def total18(self):
    if self.cost_rs18 or self.qty__m_18:
        self.total_18 = self.cost_rs18 * self.qty__m_18

cost_rs18 = fields.Float("Cost Rs.")
qty__m_18 = fields.Integer("Qty")
total_18 = fields.Float(string="Total")
#Packing
@api.onchange('cost_rs19', 'qty__m_19')
def total19(self):
    if self.cost_rs19 or self.qty__m_19:
        self.total_19 = self.cost_rs19 * self.qty__m_19

cost_rs19 = fields.Float("Cost Rs.")
qty__m_19 = fields.Integer("Qty")
total_19 = fields.Float(string="Total")
#Others
@api.onchange('cost_rs20', 'qty__m_20')
def total20(self):
    if self.cost_rs20 or self.qty__m_20:
        self.total_20 = self.cost_rs20 * self.qty__m_20

cost_rs20 = fields.Float("Cost Rs.")
qty__m_20 = fields.Integer("Qty")
total_20 = fields.Float(string="Total")
这些显然在各自的
total\n
字段中进行汇总

但另一方面,不应该总是对它们进行总结,让我自己解释一下

所有这三个字段(在我的代码中实际上是20个)都有各自的
total\u n
字段,它们将每对
cost\u r\n
quantity\u m\n
字段相加

现在我想创建一个“总计”,它应该将所有20个字段相加,这并不难计算,但是,我的观点是,并不总是将所有20个字段相加,有时是12个活动总计,有时是2个,有时是全部20个字段

我怎样才能做到这一点?我可以使用我在
@api.onchange
上使用的相同逻辑,但对所有这些逻辑都可以吗?知道并非所有人都有数据要汇总吗

编辑

这是我实际使用的函数,它汇总了我模型上所有实际字段的所有总计:

@api.onchange('total_1', 'total_2',
    'total_3', 'total_4', 'total_5', 'total_6',
    'total_7', 'total_8', 'total_9', 'total_10',
    'total_11', 'total_12', 'total_13', 'total_14',
    'total_15', 'total_16', 'total_17', 'total_18',
    'total_19', 'total_20')
def total21(self):
    if self.total_1 or self.total_2 or self.total_3 or self.total_4 or self.total_5 or self.total_6 or self.total_7 or self.total_8 or self.total_9 or self.total_10 \
    or self.total_11 or self.total_12 or self.total_13 or self.total_14 or self.total_15 or self.total_16 or self.total_17 or self.total_18 or self.total_19 or self.total_20:
        self.total_totals = self.total_1 * self.total_2 * self.total_3 * self.total_4 * self.total_5 * self.total_6 * self.total_7 * self.total_8 * self.total_9 * self.total_10 \
        * self.total_11 * self.total_12 * self.total_13 * self.total_14 * self.total_15 * self.total_16 * self.total_17 * self.total_18 * self.total_19 * self.total_20

total_totals = fields.Float(string="Total")

我已经将
default=0.00
添加到所有这些总计中,但是没有结果,
total\u totals
字段没有任何作用,有什么想法吗?

我认为您的代码工作正常。在乘法运算中,请确保所有总数都为=0.

编辑 我不确定这是最好的答案,但它将完全满足您的要求,请尝试下面的代码

@api.onchange('total_19','total_18','total_20')
def total21(self):
    total = 1
    for rec in self:
        for i in range(18,21):
            rec1 = eval('rec.total_'+str(i))
            if rec1:
                total = total * rec1
        if total !=1:
            self.total_totals=total
        else:
            self.total_totals=None

你不一定会把全部20个加起来,有时是12个有效总数,有时是2个,有时是全部20个。
你必须更具体一些,你是什么意思?什么时候应该对这些字段求和?嗨,当其中任何一个字段被填充时,无论是1、15还是20,如果这些字段中没有值,则可以将其默认值设置为零。然后不管填充了多少字段。你可以汇总所有字段。你好,NeoVe先生,试试这个,cost_rs20=fields.Float(string=“cost Rs.”,default=0.00),qty_m_20=fields.Integer(string=“qty.”,default=0)嗨,谢谢,但它不起作用,我正在编辑我的问题非常感谢,是的,这很有效,但如果有一些空的总数,我仍然想要一个活跃的总数。有没有办法做到这一点?你需要把所有的总数相加还是相乘?更新了我的答案。试试看。非常感谢,实际上我用的是20个字段,3个字段只是一个例子,你知道我怎么用它来处理20个字段吗?我可以为此提出一个新问题,只需在
@api.onchange
中传递20个参数,并将for循环的
范围更改为(1,21)。试试看。。。