2016-12-26 26 views
0
data = [{'account_type': u'account_type', 'balance': 3484382.4899999998, 'type': 'report', 'name': u'Assets', 'level': 1}, {'account_type': u'liquidity', 'balance': 87301.78, 'type': 'account', 'name': u'100101 Cash', 'level': 4}, {'account_type': u'liquidity', 'balance': 257350.98, 'type': 'account', 'name': u'100201 HDFC Bank', 'level': 4}] 
@api.multi 
def account_fun(): 
     for item in data: 
      return item['balance'] 

我打电话使用计算函数,并获得预期的单例错误。但我希望将数据中的所有余额逐个存储到数据库中。在odoo中的account.account表中。在odoo帐户树视图中预期的单身错误

balance = field.Float(string="Balance",compute="account_fun")#creating new balance field. 

我该怎么办that.and必须显示在列表视图中余额字段。谢谢,

回答

0

我不确定你想用这个逻辑来做什么,我只是在这里告诉你如何做到这一点。

data = [{'account_type': u'account_type', 'balance': 3484382.4899999998, 'type': 'report', 'name': u'Assets', 'level': 1}, {'account_type': u'liquidity', 'balance': 87301.78, 'type': 'account', 'name': u'100101 Cash', 'level': 4}, {'account_type': u'liquidity', 'balance': 257350.98, 'type': 'account', 'name': u'100201 HDFC Bank', 'level': 4}] 

@api.multi 
def account_fun(self): 
    for rec in self: 
     balance =0 
     for item in data: 
      balance += item['balance'] 
     rec.balance = balance 

balance = field.Float(string="Balance",compute="account_fun") 

你,因为记录的自我名单得到这个错误将是 那里。在新的api中,有不同的方式来设置我上面描述的功能字段 。

+0

这里只获取列表视图(每行)的值,但我需要不同的值在行中。像3484382,87301.78 – ShivaGuntuku

+0

这就是为什么我首先提到,我不明白你想要设置什么平衡。如果平衡字段是功能性的,那么它应该如何计算,您需要指定该点。但是,如上所述,您绝不会在字典列表中获取数据。 –

0
@api.multi 
def account_fun(self): 
    data = [{'account_type': u'account_type', 'balance': 3484382.4899999998, 'type': 'report', 'name': u'Assets', 'level': 1}, {'account_type': u'liquidity', 'balance': 87301.78, 'type': 'account', 'name': u'100101 Cash', 'level': 4}, {'account_type': u'liquidity', 'balance': 257350.98, 'type': 'account', 'name': u'100201 HDFC Bank', 'level': 4}] 
    for rec in self: 
     for j in range(len(data)): 
      if rec.code in data[j]['name']: 
       rec.balance= data[j]['balance'] 

balance = fields.Float(string="Balance",compute="account_fun") 

感谢@Emipro技术列兵。有些什么改变你的代码得到我想要的。