Search code examples
pythonxmlodooopenerp-8

Add column to BOM lines in OpenERP v8


I'm trying to customize the BOM form view with a custom column in the BOM lines, based on https://www.odoo.com/apps/modules/7.0/mrp_bom_product_details/

My attempt stranded at the following state:

custom_bom_form.py:

from openerp.osv import fields, osv
from openerp.tools.translate import _

class mrp_bom(osv.osv):
    _inherit = 'mrp.bom'

    _columns ={
        'product_qty_available': fields.related('product_id', 'qty_available', type='float', string='Quantity On Hand', readonly=True),
    }
    _defaults = {
    } 

mrp_bom()

custom_bom_form.xml:

<?xml version="1.0"?>
<openerp>
    <data>

        <record id="mrp_bom_form_view_product_details" model="ir.ui.view">
            <field name="name">mrp.bom.form.product_details</field>
            <field name="model">mrp.bom</field>
            <field name="inherit_id" ref="mrp.mrp_bom_form_view"/>    
            <field name="arch" type="xml">
                <xpath expr="//notebook/page[@string='Components']/field/tree[@string='Components']/field[@name='product_qty']" position="after" >
                    <field name="product_qty_available" />
                </xpath> 
            </field>
        </record>

    </data>
</openerp>

This results in an error from Odoo while installing the module:

ParseError: "ValidateError
Field(s) `arch` failed against a constraint: Invalid view definition

Error details:
Field `product_qty_available` does not exist

Error context:
View `mrp.bom.form.product_details`
[view_id: 1094, xml_id: n/a, model: mrp.bom, parent_id: 675]" while parsing /usr/lib/python2.7/dist-packages/openerp/addons/mrp_bom_product_details/mrp_bom_product_details.xml:5, near
    <record id="mrp_bom_form_view_product_details" model="ir.ui.view">
        <field name="name">mrp.bom.form.product_details</field>
        <field name="model">mrp.bom</field>
        <field name="inherit_id" ref="mrp.mrp_bom_form_view"/>    
        <field name="arch" type="xml">
            <xpath expr="//notebook/page[@string='Components']/field/tree[@string='Components']/field[@name='product_qty']" position="after">
                <field name="product_qty_available"/>
            </xpath> 
        </field>
    </record>

I can't figure out why I can't reference product_qty_available there, it works from other places in the BOM form. E.g.:

        <field name="arch" type="xml">
            <field name="product_id" position="after">
                <field name="product_qty_available" />
            </field> 
        </field>

Works.

Also instead changing the referenced field to something the system knows works, E.g. adding another product_qty column:

        <field name="arch" type="xml">
            <xpath expr="//notebook/page[@string='Components']/field/tree[@string='Components']/field[@name='product_qty']" position="after" >
                <field name="product_qty" />
            </xpath> 
        </field>

Can someone show me what I am doing wrong. How can I reference product_qty_available there? Is there something special about the notebook page?

Update

Working py file:

from openerp.osv import fields, osv
from openerp.tools.translate import _

class mrp_bom_line(osv.osv):
    _inherit = 'mrp.bom.line'

    _columns ={
        'product_qty_available': fields.related('product_id', 'qty_available', type='float', string='Quantity On Hand', readonly=True),
    }
    _defaults = {
    }

mrp_bom_line()

and xml file:

<?xml version="1.0"?>
<openerp>
    <data>

        <record id="mrp_bom_form_view_product_details" model="ir.ui.view">
            <field name="name">mrp.bom.form.product_details</field>
            <field name="model">mrp.bom</field>
            <field name="inherit_id" ref="mrp.mrp_bom_form_view"/>    
            <field name="arch" type="xml">
                <xpath expr="//field[@name='bom_line_ids']/tree/field[@name='product_qty']" position="after">
                    <field name="product_qty_available" />
                </xpath>
            </field>
        </record>

    </data>
</openerp>

Solution

  • You have added the field product_qty_available in the model mrp.bom and you are trying to add the field in the inside the field bom_line_ids - in the tree view of bom_line_ids which is one2many of mbrp.bom.line, so system tries to find the field product_qty_available over there in mrp.bom.line model which is actually not there.

    I am not sure, what is your exact aim but, try the following

    1.The field is added in the model mrp.bom so you should try the following

    <xpath expr="/form/group/group[1]/field[@name='product_id']" position="after">
        <field name="product_qty_available"/>
    </xpath>
    

    so this will add field after product_id field in the top section of screen where it shows master information.

    2.Add the field in the mrp.bom.line model and then try the following:

    <xpath expr="//field[@name='bom_line_ids']/tree/field[@name='product_qty']"  position="after">
        <field name="product_qty_available"/>
    </xpath>