【问题标题】:expected singleton error in odoo accounts tree viewodoo 帐户树视图中的预期单例错误
【发布时间】:2016-12-26 13:43:09
【问题描述】:
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 表中。

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

我该怎么做。并且必须在列表视图中显示余额字段。谢谢,

【问题讨论】:

    标签: python-2.7 openerp odoo-9


    【解决方案1】:

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

    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 中有不同的方法来设置功能字段 我已经在上面描述了。

    【讨论】:

    • 这里只获取列表视图中的值(每一行),但我需要不同的行中的值。像 3484382,87301.78
    • 这就是为什么我首先提到的原因,我不明白你想要设置什么平衡。如果 balance 字段有效,那么应该如何计算它,您需要指定该点。但是,您永远不会像您所描述的那样在字典列表中获取数据。
    【解决方案2】:
    @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 Technologies Pvt。有限公司,一些改变你的代码得到我想要的东西。

    【讨论】:

      猜你喜欢
      • 2021-11-15
      • 1970-01-01
      • 1970-01-01
      • 2020-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多