Skip to Content
Menu
This question has been flagged
1 Reply
4770 Views
Hi,
How can I add a field in a view using python code only (without using XML code)?
For example, I need to add a field in the product master form view without using any XML code.
I'm using odoo13
Avatar
Discard
Best Answer

Hi,

For adding a field using python you have to create a record in the model ir.ui.view with necessary details(position, type of field etc..).For adding a field in the product form you can try the code below in your function.

inherit_id = self.env.ref('product.product_template_only_form_view')
arch_base = _('<?xml version="1.0"?>'
'<data>'
'<field name="barcode" position="after">'
'<field name="new_field"/>'
'</field>'
'</data>')

self.env['ir.ui.view'].sudo().create({'name': 'product.inherit.fields',
'type': 'form',
'model': 'product.template',
'mode': 'extension',
'inherit_id': inherit_id.id,
'arch_base': arch_base,
'active': True})

Avatar
Discard