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

how write a onchange function?

By
nasim
on 5/14/16, 3:13 AM 384 views

hi

I want add a function in customer payment form that when I select a partner, a field withe name 'section_id' be automatically selected.

for this I add a field with name 'section_id'(many2one) with relation crm_case_section table.

and in account_voucher module, I add this code in onchange_partner_id function:

------------------------------------------------

    def onchange_partner_id(self, cr, uid, ids, partner_id, journal_id, amount, currency_id, ttype, date,section_id, context=None):

        section_id = False

        if not journal_id:

            return {}

        res = self.recompute_voucher_lines(cr, uid, ids, partner_id, journal_id, amount, currency_id, ttype, date, context=context)

        vals = self.recompute_payment_rate(cr, uid, ids, res, currency_id, date, ttype, journal_id, amount, context=context)

        for key in vals.keys():

            res[key].update(vals[key])

        return res

        if partner_id:

            partner = self.pool.get('res.partner').browse(cr, uid, partner_id)

                    value['section_id'] = partner.section_id.name

        return {'value': value}
---------------------------------------------------------------

but when I select a partner, 'section_id' field does not select. I don't know what is problem?


Thanks

you had taken partner.section_id.name so that not working please use this one: partner.section_id and partner.section_id.id or False or you can use code from my answer.

Krupesh Laiya
on 5/14/16, 5:44 AM
0
PradaOverflow
On 5/16/16, 8:34 AM

use the decorator @api.constraints of the api v8

0
Krupesh Laiya
On 5/14/16, 5:42 AM

call the super method instead of overriding the existing one see below code:

   

 def onchange_partner_id(self, cr, uid, ids, partner_id, journal_id, amount, currency_id, ttype, date, context=None):
     res = super(account_voucher, self).onchange_partner_id(cr, uid, ids, partner_id, journal_id, amount, currency_id, ttype, date, context=context)
     if not partner_id:
        return res
     partner = self.pool.get('res.partner').browse(cr, uid, partner_id) 
     res['value'].update({'section_id': partner.section_id and partner.section_id.id or False})
     return res

where account_voucher: is the class name in super calling

           

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: 5/14/16, 3:13 AM
Seen: 384 times
Last updated: 5/16/16, 8:34 AM