Diego Calzadilla
Diego Calzadilla

Reputation: 309

onchange method not working (no error displayed) odoo

I have defined these classes:

class brand(osv.osv):
    _name = "cm.brand"
    _columns = {
        'name': fields.char('Marca', size=30)
        }

class model(osv.osv):
     _name = "cm.model"
     _columns = {
        'name': fields.char('Modelo', size=30)
         }

class cm_application_data_line(osv.osv):
      _name = "cm.application.data.line"
      _columns = {
        'comun_denominador_id': fields.many2one('comun.denominador', 'Comun denominador', required=True, ondelete='cascade'),
        'brand': fields.many2one('cm.brand', 'Marca', required=True, ondelete='restrict'),
        'date_beg': fields.selection([(num, str(num)) for num in range(1980, (datetime.now().year)+1 )], 'Año inicial'),
        'dateend': fields.selection([(num, str(num)) for num in range(1980, (datetime.now().year)+1 )], 'Año final'),
        'model': fields.many2one('cm.model', 'Modelo', required=True, ondelete='restrict')
         }

I did this to get a page view on my 'comun.denominador' new model:

class comun_denominador(osv.osv):
    _name='comun.denominador'
    _rec_name='comun_denominador'
    _columns = {       
        'comun_denominador': fields.char('Común denominador', size=10),
        'application_data_comun_denominador_ids': fields.one2many('cm.application.data.line', 'comun_denominador_id', 'Datos de aplicación del común denominador')

       }

And xml view:

<notebook>
    <page string="Datos de aplicación">
        <field name="application_data_comun_denominador_ids" widget="one2many_list" context="{'show_attribute': False}">
            <tree string="Aplicación" editable="bottom">
                <field name="brand" />
                <field name="model" /> 
                <field name="date_beg"/>
                <field name="dateend"/>
            </tree>
        </field>
    </page>
</notebook>

So far, everything is fine. Now, in product.template form view I added exactly the same page in form view and added a field 'cm_id' making reference to 'comun.denominador' model, so that when users chooses 'cm_id', on_change method brings all values from page in 'comun.denominador' to my new page in 'product.template':

XML sample in product.template:

<xpath expr="//field[@name='sale_ok']" position="after">
    <separator colspan="2" string="Común denominador"/>
        <field name="cm_id" on_change="on_change_cm_id(cm_id)"/>
</xpath>
<xpath expr="//page[@string='Sales']" position="after">
    <page string="Datos de aplicación">
        <field name="application_data_product_template_ids" widget="one2many_list" context="{'show_attribute': False}">
            <tree string="Aplicación" editable="bottom">
                <field name="brand" />
                <field name="model" /> 
                <field name="date_beg"/>
                <field name="dateend"/>
            </tree>
        </field>
      </page>
 </xpath>

New class and product.template inheritance:

class product_template_application_data_line(osv.osv):
    _name = "product.template.application.data.line"
    _rec_name = 'brand'
    _columns = {
        'product_template_id': fields.many2one('product.template', 'Product template', required=True, ondelete='cascade'),
        'brand': fields.many2one('cm.brand', 'Marca', required=True, ondelete='restrict'),
        'date_beg': fields.selection([(num, str(num)) for num in range(1980, (datetime.now().year)+1 )], 'Año inicial'),
        'dateend': fields.selection([(num, str(num)) for num in range(1980, (datetime.now().year)+1 )], 'Año final'),
        'model': fields.many2one('cm.model', 'Modelo', required=True, ondelete='restrict')

   }


class comun_denominador_product_template(osv.osv):
    _inherit = 'product.template'
    _name='product.template'
    def on_change_cm_id(self,cr, uid, ids,cm_id,context=None):
      context=context or {}
      application_data_product_template = []
      dict = {}
      if ids:
       application_ids = self.pool.get('product.template.application.data.line').search(cr, uid,[('product_template_id','in',ids)])
       self.pool.get('product.template.application.data.line').unlink(cr, uid, application_ids)
      application_cm_ids = []
      application_cm_ids = self.pool.get('cm.application.data.line').search(cr, uid, [('comun_denominador_id', '=', cm_id)])
      for application_id in self.pool.get('cm.application.data.line').read(cr, uid, application_cm_ids, ['brand', 'date_beg', 'dateend', 'model']):
          application_data_product_template.append((0,0,{'brand':application_id['brand'][0],'date_beg':application_id['date_beg'], 'dateend':application_id['dateend'], 'model':application_id['model'][0]})) 
      dict.update(application_date_product_template_ids=
                  application_data_product_template)              
      return {'dict':dict}
_columns = {
    'cm_id' : fields.many2one('comun.denominador','Común denominador', select=True, ondelete='cascade'),
    'application_data_product_template_ids': fields.one2many('product.template.application.data.line', 'product_template_id', 'Datos de aplicación de la tabla de producto')
      }

When I select cm_id, I see no error log, what is more, setting up debugger my on_change function returns a dict like this:

dict: {'application_data_product_template_ids': [(0, 0, {'brand': 2, 'date_beg': 1995, 'dateend': 2009, 'model': 5}), (0, 0, {'brand': 1, 'date_beg': 1995, 'dateend': 2006, 'model': 2})]}

Which seems to be correct.

However, my page values on product.template don't get uploaded.

Please, some suggestions!!

Upvotes: 0

Views: 1434

Answers (1)

Arslan Rafique
Arslan Rafique

Reputation: 176

According to docs given by OpenERP the definition of on_change function should be like:

def name_change(self, cr, uid, ids, name, address, city, context=None):
    ...
    return {
        'value': {
            'address': ...
            'city': ...
        }
    }

Here in return, 'value' is the main key and in your case 'dict' is the main key, so thats why your on_change is not working.

Have a look at this page for more detail.

Hope it helps.

Upvotes: 1

Related Questions