4
votes

Using an on_change method I am trying to send default values to a one2many field using the concept of (0,0, {list values}; even there it works fine but inside that one2many there is also a one2many field to which I must also send values to implement In my code the same concept of (0,0, {list values}, but I did not get success.

Could you help me?.

Attached fragment of my code

class hc(models.Model):
    _name = 'hc'
    laboratorios_ids = fields.One2many(
        string='Laboratorios', comodel_name='resullab',
        inverse_name='hc_id')

    def cargar_pool_labs(self):
        labs = []
        pool = self.env['mymodel'].sudo().search([])
        for laboratorio in pool:
            analitos = [(0, 0, {'analito': line.analito,
                                'resultado': line.resultado ,
                                'resultado2': line.resultado2 ,
                                'unidades': line.unidades })
                            for line in laboratorio.analitos_ids]
            labs.append((0,0,{
            'nombre_examen' : laboratorio.nombre_examen,
            'cups' : laboratorio.cups,
            'laboratorio_id': laboratorio.laboratorio_id,
            'analitos_ids' : analitos
            }))
        self.laboratorios_ids = labs

Note: the analitos_ids field is the one2many field that is inside the laboratorios_ids field which is also one2many.

The others models extructure

class resullab(models.Model):
    _name = 'resullab'
    analitos_ids = fields.One2many('analito','laboratorio_id',
                                   'Analitos Lab', ondelete='cascade')

class labs_analito(models.Model):
    _name = 'analito'
    laboratorio_id = fields.Many2one('laboratorios', 'Lab Parent')

Thanks for help!

2
Please specify model structures, which many2one fields are there.Emipro Technologies Pvt. Ltd.
@EmiproTechnologiesPvt.Ltd. Thanks for replying, I have added more information on the models.Gustavo Salgado
I have updated answer, review it.Emipro Technologies Pvt. Ltd.

2 Answers

3
votes

The only reason I can see here is the missing many2one reference in analitos. This 'analitos_ids' : analitos refers to which many2one model that record id must be specified.

def cargar_pool_labs(self):
  labs = []    
  pool = self.env['mymodel'].sudo().search([])
  for laboratorio in pool:
      analitos = [(0, 0, {'analito': line.analito, 'resultado': line.resultado , 'resultado2': line.resultado2 , 'unidades':            line.unidades, }) for line in laboratorio.analitos_ids]
      labs.append((0,0,{'nombre_examen' : laboratorio.nombre_examen,
     'cups' : laboratorio.cups, 'laboratorio_id': laboratorio.laboratorio_id.id, 'analitos_ids' : analitos}))
  self.laboratorios_ids = labs

And one more thing many2one reference must be id not an object. In your code it's an object not ID.

'laboratorio_id': laboratorio.laboratorio_id.id

Solution:

The many2one field 'hc_id' in resullab model is missing.

'hc_id' : self.id #(here the id of 'hc' model required).

analitos = [(0, 0, {'hc_id': id of resullab_object.id})]

This might helps you.

Filling default value for one2many model

1
votes

sorry about my english.

1- onchange is a method called from the client side on the server this method will return a value to the client side (javascript).

2- so when the client receive this data will fillup the fields so in the current view that you are in (form) if the field is not there the value will be ignored.

3- so when you pass the values of the second o2m field in the view this field is not there because it's in the form that will be opened by clicking on additem or on the item to edit. and this is why the value are not shown in the the form when you open the item.

There is no way to do this in onchange event because there is no way to pass the list of the field to the tree of the first o2m. what i mean is :

        o2m1_id            o2m2_id
model.1 -------> model.2 --------> model.3(field1, field2)

in the view of model.1 you can create a special tree for o2m1_id and put the field o2m2_id there. but this is useless because when you open the form fo o2m1_id you will find that the o2m2_id contains records but with empty (field1, field2). and this is because values of field1, field2 returned by onchange event are ignored, this two field are not in the view of model.1 that receives them.