Danny Web
Danny Web

Reputation: 281

odoo 10 - @api.onchange

I am having difficulty in getting the @api.onchange to work.

here are my files

init.py

from . import models

manifest.py

# -*- coding: utf-8 -*-
{
    'name': "Sales Test",
    'description': 'test description',
    'depends': ['sale'],
    'category': 'Test',
    'data': [
        'views/templates.xml',
    ],
    'installable': True,
    'application': False,
    'auto_install': False,
}

init.py (file under models directory)

from . import partner

partner.py (file under models directory)

# -*- coding: utf-8 -*-
from odoo import models, fields, api

class MyPartner(models.Model):
    _inherit = 'res.partner'

    @api.onchange('country_id')
    def _onchange_country_id(self):
        self.name = 'OnChange'


    @api.onchange('street', 'zip')
    def _onchange_street(self):
        self.street = 'Test Street'
        return {
            'warning': {
                'title': "Some changes happened",
                'message': "onchange working, bravo!!!",
            }
        },

the module is getting installed without any problem and the view is also been altered according to the templates.XML, but nothing happens when change happens to the fields (street, country_id or zip)

Upvotes: 4

Views: 7002

Answers (4)

KbiR
KbiR

Reputation: 4174

Try this:

@api.onchange('country_id')
@api.depends('country_id')
def _onchange_country_id(self):
    print'Am here!!'
    self.name = 'OnChange'

Upvotes: 0

Pawan Kumar Sharma
Pawan Kumar Sharma

Reputation: 1168

Try this by first importing model and api:

from openerp import api, fields, models, _

Also for odoo 10 you have to be call your onchange function in the field where you try to apply onchange for eg.

@api.onchange('product_id')
   def _onchange_product_pack_name(self):
   res = self.product_id.product_pack
   if res:
      return {'domain': {'pack_id': [('id', 'in', [v.id for v in res])]}}

test = fields.Many2one('product.product',string="Pack Products",change_default=True, default=_onchange_action_product_add )

Upvotes: 1

Danny Web
Danny Web

Reputation: 281

I have also tried to make a simple custom logging, it doesnt work too...

# -*- coding: utf-8 -*-
from odoo import models, fields, api, _

class MyPartner(models.Model):
    _inherit = 'res.partner'

@api.onchange('street', 'zip')
def _onchange_street(self):
    save_path = 'C:/temp/odoo.txt'
    file1 = open(save_path, "w")
    toFile = raw_input("street field changed!")
    file1.write(toFile)
    file1.close()       
    self.street = 'Test Street'
    warning = {}
    result = {}
    warning = {
        'title': _('Some changes happened!'),
        'message': _('onchange working, bravo!!!'),
    }
    if warning:
        result['warning'] = warning
    return result

tried restarting services also.. no help!

Upvotes: 1

Bhavesh Odedra
Bhavesh Odedra

Reputation: 11143

At the end of return statement, you have written , that's mistake. Other then your code seems to me good. Just make sure it installed perfectly.

I shared my style of onchange() method code.

@api.onchange('street', 'zip')
def _onchange_street(self):
    self.street = 'Test Street'
    warning = {}
    result = {}
    warning = {
        'title': _('Some changes happened!'),
        'message': _('onchange working, bravo!!!'),
    }
    if warning:
        result['warning'] = warning
    return result

NOTE:

These requires to import _ For example.

from odoo import models, fields, api, _

Upvotes: 1

Related Questions