Filling one in one of them in onchange

Using the on_change method, I am trying to send default values ​​to the one2many field using the concept of (0,0, {list values}, even there it works fine, but inside this object there is one more many field to which I must also send values ​​for implementations. My code uses the same concept (0,0, {list values}, but I did not get success.

could you help me?

Attached Code Snippet

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 a single field that is inside the labatorios_ids field, which is also equal to a number.

Other models expand

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

+4
source share
2 answers

, , - many2one analitos. "analitos_ids": analitos , many2one, .

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

many2one reference id, . ID.

'laboratorio_id': laboratorio.laboratorio_id.id

:

many2one 'hc_id' resullab.

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

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

.

2

+3

.

1- onchange - , .   (javascript).

2-, , , ,  , (), ,  .

3-, , o2m ,   , , additem   .   .

onchange, o2m. :

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

model.1 o2m1_id o2m2_id. , , fo o2m1_id, , o2m2_id , (1, 2). , 1, field2 onchange , model.1, .

+1

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


All Articles