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.

1

onchange method not working (no error displayed)

By
Diego Calzadilla
on 4/2/15, 12:07 PM 597 views

Hi, 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_data_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})]}


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

Please, some suggestions!!

0
Diego Calzadilla
On 4/3/15, 12:13 AM

I answer my own question:

The problem is that I called my dictionary : 'dict', this is not allowed, it must be named 'value' according to documentation!

Hope this is helpful for someone else!

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: 4/2/15, 12:07 PM
Seen: 597 times
Last updated: 1/19/16, 1:33 AM