Odoo Help

Welcome!

This community 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.

0

Functionnal field : struglling with return type v7

By
Yassine TEIMI
on 12/29/14, 2:33 PM 664 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

--Ivan--
3210
| 5 3 6
Jakarta, Indonesia
--Ivan--
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

--Baiju--
3154
| 5 4 8
Kochi, India
--Baiju--
ODOO technical and functional

        I like learning new thing, especially technical thing. I am a quick learner, I can learn technical things very fast. I have good logical skill,  I have immense interest in programming.

e-mail id : baijuks@hotmail.com

Baiju
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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

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