expected singleton error in odoo accounts tree view

265 Views Asked by At
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']

i'm calling that function using compute and getting the error expected singleton. but i want store all the balance from data into the database by one by one. in account.account table in odoo .

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

how can i do that.and have to show balance field in list view. thanks,

2

There are 2 best solutions below

2
On

I am not sure what you are trying to do with that logic, I have just give you idea here how you can do it.

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")

You are getting this error because in self list of recordset will be there. In new api there is different way to set the functional field which I have described above.

0
On
@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")

thanks @Emipro Technologies Pvt. Ltd. ,some what changed your code getting what i want.