Odoo Help

0

Functionnal field : struglling with return type v7

By
Yassine TEIMI
on 12/29/14, 2:33 PM 880 views

I am on account_voucher model I want to change the amount field's type into functionnal field, so I did :

class AccountVoucher(orm.Model):

    _inherit="account.voucher"
    _name="account.voucher"

    def _total_payed(self, cr, uid, ids, amount, arg, context=None):
        res = {}
        voucher = self.browse(cr, uid, ids[0], context=context)
        for line in voucher.line_cr_ids:
            res[voucher.id] += line.amount
        return res

    _columns = {
        'amount': fields.function(_total_payed, method=True, string='Amount payed', type='float'),
    }

The purpose is to change it to readonly=True, and make it compute the sum of all the amount payed for all the voucher lines displayed for the current selected customer.

Error Traceback : 

File "/opt/openerp/server/openerp/osv/orm.py", line 3800, in _read_flat res2 = self._columns[f].get(cr, self, ids, f, user, context=context, values=res) File "/opt/openerp/server/openerp/osv/fields.py", line 1145, in get result = self._fnct(obj, cr, uid, ids, name, self._arg, context) File "/opt/openerp/server/openerp/addons/account_payment_bl/account_voucher.py", line 56, in _total_payed res[voucher.id] += line.amount KeyError: 5616 

what's wrong with the key ?

0
Ivan
On 12/30/14, 12:18 AM

@Yassine, you need to make a loop of ids instead of just taking the first member.  So:

def _total_payed(self, cr, uid, ids, amount, arg, context=None):
        res = {}
        for voucher in self.browse(cr, uid, ids, context=context):

            res[voucher.id] = 0.0
            for line in voucher.line_cr_ids:
                res[voucher.id] += line.amount
        return res

The key error message is caused if ids contains more than one ids.  So, the 2nd and remaining ids wouldn't exist in res because the original code only contain the first member of ids.

Yes ! it worked, many thanks !

Yassine TEIMI
on 12/30/14, 6:20 AM
0
Baiju KS
On 12/29/14, 11:57 PM

Hi Yassine,

Here return value have to be a dictionary in the following format.

res[record.id] = {
                'field_name': value,
            }

In your case you can code like this;-

        res[voucher.id] = {
                'amount': 0.0,
            }

        for line in voucher.line_cr_ids:
            res[voucher.id]['amount'] += line.amount

        return res

       

Thanks Baiju

Yassine TEIMI
on 12/30/14, 7:07 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)

Stats

Asked: 12/29/14, 2:33 PM
Seen: 880 times
Last updated: 3/16/15, 8:10 AM