创建新的记录odoo

时间:2017-12-06 16:33:41

标签: openerp odoo-10

我想在函数create中仅从rubrique_ids返回所有rubrique_name(nom_rubrique),但它只返回最后一个并希望返回所有的criter_name(nom_critere)

def evaluer(self):
    self.ensure_one()
    rub = self.risque.rubrique_ids
    for rec in rub:
       rubriq = rec.nom_rubrique
       critere_ids = rec.critere_ids
           for obj1 in critere_ids:
               crit = obj1.nom_critere
               coeff = obj1.coefficient
           rubrique_etude = rubriq
    test = self.env['risque.evaluation'].create(
        {
            'risque_etude': self.risque.nom_risque, 
            'etude': [(0, 0, { 
                'rubrique_etude': rubrique_etude, 
                'critere_etude': [(0, 0, {
                     'critere_name': crit, 
                     'coefficient': coeff, 
                     'critere_evaluation': '', 
                     'note': '' })], 
                })]
        }
    )

    return {
        'name': 'Evaluation',
        'view_type': 'form',
        'view_mode': 'tree,form',
        #'views': [{'view_mode': 'form', 'view_id': 'rub_id'}],
        'res_model': 'risque.evaluation',
        'type': 'ir.actions.act_window',
        'res_id': test.id,
        #'target': 'new',
        'flags': {'initial_mode': 'edit'},
        'context': self.env.context,
    }

1 个答案:

答案 0 :(得分:3)

它只保留最后一行,因为你不先将它们保存在列表中:

    def evaluer(self):
        self.ensure_one()
        rub = self.risque.rubrique_ids
        for rec in rub:
            rubriq = rec.nom_rubrique
            critere_ids = rec.critere_ids
            # create a list to hold the critere_etude records
            critere_etude = []
            for obj1 in critere_ids:
               critere_etude.append((0, 0,{
                    'critere_name':obj1.nom_critere,
                    'coefficient':obj1.coefficient,
               }))
            rubrique_etude = rubriq
            test = self.env['risque.evaluation'].create({'risque_etude': self.risque.nom_risque,
                                               'etude': [(0, 0, {'rubrique_etude': rubrique_etude,
                                                                 # and here pass the whole list
                                                                 'critere_etude': critere_etude,
                                             })]
                            })