Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
4669 Lượt xem

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!!

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

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!

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 12 18
13
1
thg 5 16
4367
3
thg 6 19
7403
1
thg 9 23
6343
1
thg 8 22
7647