我在“组件”选项卡的“物料清单”中创建了名为product_unit_cost的新列。
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
'product_unit_cost' : fields.function(_get_unit_cost, type='many2one', relation="product.product", string="Product Unit Cost"),
<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>
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: )>"
请帮忙!如何在product.product中获取id字段?请指教。谢谢。
答案 0 :(得分:2)
问题在于你的函数_get_unit_cost你的功能领域。在这个函数中,你已经编写了用条件[('product_id','=',uid)]来搜索模型。但我不确定您为什么要使用用户ID搜索product_id。 uid是指当前登录用户的id。如果要获取正在执行功能字段的当前产品的ID,可以使用ids字段。即;您可以将代码修改为:
def _get_unit_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
return result