Skip to Content
Menu
This question has been flagged
1 Reply
9449 Views

I have create new column called product_unit_cost in Bill Of Materials in the Components tab.

HERE IS MY FUNCTION:

def _get_unit_cost(self, cr, uid, ids, field_name, arg, context):
        product_ids = self.pool.get('product.product').search(cr, uid, [('product_id', '=', uid)], context=context)
        prod = self.pool.get('product.product')
        prodobj = prod.browse(cr, uid, product_ids[0])
        res = prodobj.standard_price
        result = {}
        for id in ids:
            result[id] = res
        return result

HERE IS MY COLUMN FIELD:

'product_unit_cost' : fields.function(_get_unit_cost, type='many2one', relation="product.product", string="Product Unit Cost"),

HERE IS MY XML FILE:

<page string="Components">
                            <field name="bom_lines" widget="one2many_list">
                                <tree string="Components" editable="bottom">
                                    <field name="product_id" context="{'default_supply_method':'produce'}" on_change="onchange_product_id(product_id, name)"/>
                                    <field name="product_qty"/>
                                    <field name="product_uom" on_change="onchange_uom(product_id, product_uom)" groups="product.group_uom"/>
                                    <field name="product_unit_cost"/>
                                    <field name="name" invisible="1"/>
                                    <field name="date_start"/>
                                    <field name="date_stop"/>
                                </tree>
                            </field>
                        </page>

Then it show me this error when I run:

File "/home/henry/openerp/7.0_20-09-13/server/openerp/osv/expression.py", line 642, in __init__
    self.parse(cr, uid, context=context)
  File "/home/henry/openerp/7.0_20-09-13/server/openerp/osv/expression.py", line 806, in parse
    raise ValueError("Invalid field %r in leaf %r" % (left, str(leaf)))
ValueError: Invalid field 'product_id' in leaf "<osv.ExtendedLeaf: ('product_id', '=', 1) on product_product (ctx: )>"
2013-11-05 02:25:13,890 8079 ERROR SydneyCakeHouseDB openerp.netsvc: Invalid field 'product_id' in leaf "<osv.ExtendedLeaf: ('product_id', '=', 1) on product_product (ctx: )>"

ValueError: Invalid field 'product_id' in leaf "<osv.ExtendedLeaf: ('product_id', '=', 1) on product_product (ctx: )>"

Please help! How can I get the id field in product.product? Please advise. thank you.

Avatar
Discard

your function is wrong. You have assigned uid for product_id in search method. Remove that first. And can you explain on what condition are you wishing to access product_id in that function _get_unit_cost()?

Author

Hi Abhishek, thank you for your reply. Actually i want the "Product Unit Cost" to be display in the bom_lines tree in the "Bill of Materials". The "Product Unit Cost" actually i want direct get from product.product table with the field name standard_price based on the product_id in the current bom_lines. So, how should I code my function? Please advise.

Author Best Answer

I have solved my issue:

def _get_total_cost(self, cr, uid, ids, field_name, arg, context):
        result = {}

        for bom_line_obj in self.browse(cr, uid, ids, context=context):
            result[bom_line_obj.id] = (bom_line_obj.product_id.product_tmpl_id.standard_price or 0.00) * (bom_line_obj.product_qty or 0.00)
        return result
Avatar
Discard