Return 动态表单

Return form dynamically

您好,请帮忙,我怎样才能动态地 return 新形式的 One2many 值。

我有三个 class 我定义了一个函数,其中我 return many2many(rubrique_ids) 和 One2many(critere_ids) 的值最近以新形式存在我如何 return(generate) 从 One2many 字段的值动态生成表单

文件xml

    <button name="evaluer" string="Evaluer" type="object"/>

第一class

    class A(models.Model):
        _name='risques.risque'
        _rec_name='nom_risque'

        nom_risque = fields.Char(required=True, string="Nom du Risque")
        rubrique_ids = fields.Many2many('risques.rubrique', string="Rubrique", required=True)

第二个 class:

    class B(models.Model):
        _name='risques.rubrique'
        _rec_name='nom_rubrique'

        critere_ids = fields.One2many('risques.critere','rubrique_id',required=True, string="Critére d'évaluation")
        nom_rubrique = fields.Char(string="Rubrique")

第三个class

    class C(models.Model):
        _name='risques.critere'
        _rec_name='nom_critere'

        nom_critere = fields.Char(string="Nom du Critere")
        note = fields.Float(string="Note", readonly=True, compute='_onchange_mesure')
        rubrique_id = fields.Many2one('risque.rubrique',
                                    string='Rubrique')


    class etude(models.Model):
        _name = 'etude.risque'
        client = fields.Many2one('res.partner', string="Client",required=True, index=True)
        risque = fields.Many2one('risques.risque', string="Risque",required=True)
        def evaluer(self):
            context = self._context.copy()
            rubriques=self.risque.risque_id
            nom_rub=[]
            rubs = self.env['risques.rubrique'].browse(rubriques)
            critere_etude =[]
            my_vals = []
            for a in enumerate(rubs):

                i = a[0]
                r = self.env['risques.rubrique'].search([('id','=',self.risque.risque_id[i].id)])
                cr = self.risque.risque_id[i].critere
                for b in enumerate(cr):
                    j = b[0]
                    critere_etude.append(cr.search([('id','=',cr[j].id)]).nom_critere)
                    print cr.search([('id','=',cr[j].id)]).id

                nom_rub.append({'etude_id' : 1,
                                'rubrique_etude':r.nom_categorie,
                                'critere_etude':critere_etude
                                })
                critere_etude = []
                my_vals.append(nom_rub)
                nom_rub = []

            self.env['risque.evaluation'].create({'risque_etude': self.risque.nom_risque,
                                                  'etude': my_vals})

            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': self.id,
                #'target': 'new',
                'context': context,
            }

我如何return rubrique_ids 和 critere_ids 动态地

首先,当您搜索表单用户时:

  form_id = self.evn.ref('addon_name.form_xml_id').id # form_id must be integer value not object
  # ex:   form_id = self.env.ref('sale.some_form_id').id
  # ex:   form_id = self.env.ref('point_of_sale.some_other_form_id').id
  # same thing for any view always look for it by the xml id.

如果您在一个视图中打开记录,现在当您在 return 中打开操作时 例如在表单视图中:

     form_id = self.evn.ref('addon_name.form_xml_id').id
     return {
            'name': 'Evaluation',
            'view_type': 'form',
            'view_mode': 'form',
            'view_id': form_id, 
            'res_id': record_id, # integer value if you want to open a specific record 
                                 # if not just put False or remove res_id this will open the form
                                 # in create mode but when you specify the id in edit mode
            ....
            ...
            }

现在如果你想在多个视图中打开多个记录。

        form_id = self.evn.ref('addon_name.form_xml_id').id
        tree_id = self.evn.ref('addon_name.tree_xml_id').id
        return {
            'name': 'Evaluation',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'view_id': form_id,
            'views': [(tree_id, 'tree'), (form_id, 'form')] # list of tuple (id, view_code)
            'domain': [some_domain], # when you open more than one record we don't user res_id
                                     # we use domain to filter just those record that we want ot see
            .....
            .....

希望这能让您了解如何向用户指定 视图: 注意:对于搜索视图:

      'search_view_id': search_id, # integer value don't pass the object will give json error in client