odoo dynamically add fields and view dynamically add fields

In some cases, it is necessary to dynamically add fields A, B, C..., and dynamically add to the tree view display, because it is followed by the field added later, it needs to be written with xpath

  • The first step is to add fields to the model.
    Example: dynamically add fields to the account.move.line model
model = self.env['ir.model'].search([('model', '=', 'account.move.line')], limit=1) #找到记录account.move.line模型的记录
fields_name = 'x_wg_%s_id' % (res.model_id.model.replace('.', '_')) #字段name
new_field = self.env['ir.model.fields'].create({
    
    
            'model_id': model.id, #添加到model模型的字段
            'name': fields_name,  #字段name
            'field_description': res.name, #字段string
            'ttype': 'many2one',    #字段关系(任意都是可以的)
            'relation': res.model_id.model,  #many2one关联的模型(根据ttype来确认是否关联)
        })
  • Add fields to the view
    Example: add fields to the pivot view of account.move.line
fields_arch = '<field name="{}" type="col"/>'.format(fields_name) #拼接xml字段格式
arch = u"""<xpath expr="//field[@name='product_id']" position="after">{}</xpath>""".format(fields_arch) #拼接xpath格式
self.env['ir.ui.view'].create({
    
    
            'model': 'account.move.line', #往哪个模型添加
            'inherit_id': self.env.ref("weigan_general_ledger.wg_move_line_report_pivot").id, #继承到哪个视图(模块.视图id)
            'mode': 'primary', #模式
            'arch': arch, #具体操作引进来
        })

In this way, it is possible to dynamically add fields and dynamically add fields to a view (any view can be used)

The complete code is as follows:
    @api.model
    def create(self, vals):
        res = super(AccountAnalyticDimension, self).create(vals)
        model = self.env['ir.model'].search([('model', '=', 'account.move.line')], limit=1)
        fields_name = 'x_wg_%s_id' % (res.model_id.model.replace('.', '_'))
        new_field = self.env['ir.model.fields'].create({
    
    
            'model_id': model.id,
            'name': fields_name,
            'field_description': res.name,
            'ttype': 'many2one',
            'relation': res.model_id.model,
        })
        fields_arch = '<field name="{}" type="col"/>'.format(fields_name)
        arch = u"""<xpath expr="//field[@name='product_id']" position="after">{}</xpath>""".format(fields_arch)
        self.env['ir.ui.view'].create({
    
    
            'model': 'account.move.line',
            'inherit_id': self.env.ref("weigan_general_ledger.wg_move_line_report_pivot").id,
            'mode': 'primary',
            'arch': arch,
        })
        return res

The example of the complete code above illustrates:
when the data of this class (AccountAnalyticDimension) is created, the field will be dynamically created based on the data inside, and then the field will be added to the pivot view, that is, create a piece of data, dynamically add a field and then automatically add it to the view .

Of course, you can also create a new view completely, that is, dynamically create a completely new view.
Please refer to the previous article

Guess you like

Origin blog.csdn.net/weixin_42464956/article/details/109545171