Search code examples
pythonpython-3.xodooodoo-14

How To Add Custom Fields to Existing Views in Odoo 12 With Addons?


I'm totally new to Odoo Add-ons (custom module) development and i'd like to know how can i possibly add a custom models to an existing views without creating a custom views to inherit it.

My goal is to create a Boolean and new field that will show in product.template as picture below :

enter image description here

but once i'm upgrading my custom module, it's says :

Field "x_show_in_ecommerce" does not exist in model "product.template"

My models named sempoaEcommerce.py

from odoo import api, fields, models


class SempoaEcommmerce(models.Model):
    _name = "sempoa.ecommerce"
    _description = "Sempoa Ecommerce"

    x_show_in_ecommerce = fields.Boolean()
    x_description_sale_ecommerce = fields.Text()

And here's my sempoaEcommerce.xml file

    <record id="product_template_only_form_view_inherit" model="ir.ui.view">
        <field name="name">product.template.inherited</field>
        <field name="model">product.template</field>
        <field name="inherit_id" ref="product.product_template_only_form_view" />
        <field name="arch" type="xml">

            <field name="sale_ok" position="after">
                <field name="x_show_in_ecommerce"/>
                <field name="x_description_sale_ecommerce"/>
            </field>

        </field>
    </record>

I'm using Odoo 14 to develop this custom module. Any advice and help is really, really appreciated!


Solution

  • Views define the way the records of a model are displayed, to display your field in a model form view you have to add the field to that model (product.template).

    Alter the product.template model to add x_show_in_ecommerce and x_description_sale_ecommerce fields:

    class ProductTemplate(models.Model):
        _inherit = 'product.template'
    
        x_show_in_ecommerce = fields.Boolean()
        x_description_sale_ecommerce = fields.Text()
    

    sale_ok is defined inside a div and they defined its label manually, your two fields will be displayed without labels.