Odoo 10 - @ api.onchange

I find it difficult to work @ api.onchange.

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 in the model directory)

from . import partner

partner.py (file in the model 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 installs without any problems, and the view also changes according to the .XML templates, but nothing happens when the change occurs with the fields (street, country_id or zip)

+4
source share
4 answers

At the end of the return statement, you wrote ,this error. Other, then your code seems good to me. Just make sure it is installed perfectly.

I shared my style with the 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:

For this you need to import _For example.

from odoo import models, fields, api, _
+1

, ...

# -*- 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

.. !

+1

api:

from openerp import api, , , _

Also for odoo 10 you need to call your sharing function in the field where you are trying to apply onchange, for example.

@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 )
+1
source

Try the following:

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

Source: https://habr.com/ru/post/1688141/


All Articles