以下是我创建的编码:
class mrp_extend(osv.Model):
_inherit = 'mrp.bom'
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
def _get_product_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
def get_total_cost(self, cr, uid, ids, name, args, context=None):
res = {}
for rec in self.browse(cr, uid, ids, context=context):
total_cost = 0.0
for line_rec in rec.bom_lines:
total_cost += line_rec.product_total_cost or 0.0
res.update({rec.id : total_cost})
return res
_columns = {
'product_unit_cost' : fields.function(_get_unit_cost, string="Product Unit Cost", digits_compute=dp.get_precision('Product Price')),
'product_total_cost' : fields.function(_get_product_total_cost, string="Total Product Unit Cost", digits_compute=dp.get_precision('Product Price')),
'total_cost' : fields.function(get_total_cost, string="Total Cost", digits_compute=dp.get_precision('Product Price')),
'mrp_bom_ids' : fields.one2many('mrp.extend.bom', 'mrp_extend_id', 'MRP Extend', states={'done': [('readonly', False)]})
}
_defaults = {
'total_cost': lambda *a: 0.0,
}
def onchange_product_id(self, cr, uid, ids, product_id, name, context=None):
""" Changes UoM and name if product_id changes.
@param name: Name of the field
@param product_id: Changed product_id
@return: Dictionary of changed values
"""
if product_id:
prod = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
return {'value': {'name': prod.name, 'product_uom': prod.uom_id.id, 'product_unit_cost': prod.standard_price}}
return {}
def onchange_uom(self, cr, uid, ids, product_id, product_uom, context=None):
res = {'value':{}}
if not product_uom or not product_id:
return res
product = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
uom = self.pool.get('product.uom').browse(cr, uid, product_uom, context=context)
if uom.category_id.id != product.uom_id.category_id.id:
res['warning'] = {'title': _('Warning'), 'message': _('The Product Unit of Measure you chose has a different category than in the product form.')}
res['value'].update({'product_uom': product.uom_id.id})
return res
class mrp_production_extend(osv.Model):
_inherit = 'mrp.production'
def _get_total_cost(self, cr, uid, ids, field_name, arg, context):
bom_point = self.pool.get('mrp.bom').browse(cr, uid, ids, context=context)
total_cost = bom_point.total_cost or 0.00
product_qty = bom_point.product_qty or 0.00
if product_qty > 1:
total_cost = total_cost / product_qty
return total_cost
_columns = {
#Add by Henry on 07Nov2013
#'total_cost' : fields.float('Total Cost', digits_compute=dp.get_precision('Product Price')),
'total_cost' : fields.function(_get_total_cost, string="Total Cost", digits_compute=dp.get_precision('Product Price')),
'pre_set_qty' : fields.float('Pre Set Quantity', digits_compute=dp.get_precision('Product Unit of Measure')),
'total_final_cost' : fields.float('Total Final Cost', digits_compute=dp.get_precision('Product Price')),
'mrp_production_ids' : fields.one2many('mrp.production.extend.bom', 'mrp_production_extend_id', 'MRP Production Extend', states={'done': [('readonly', False)]})
#-----------------------------
}
我有新的create 2对象。一个是mrp_extend,该对象继承自mrp.bom。
然后我创建了新的对象名mrp_production_extend,该对象继承自mrp.production。
现在我的问题是,我在mrp_production_extend中创建了一个函数:
def _get_total_cost(self, cr, uid, ids, field_name, arg, context):
bom_point = self.pool.get('mrp.bom').browse(cr, uid, ids, context=context)
total_cost = bom_point.total_cost or 0.00
product_qty = bom_point.product_qty or 0.00
if product_qty > 1:
total_cost = total_cost / product_qty
return total_cost
但是此功能无效并向我显示此错误:
AttributeError: 'browse_record_list' object has no attribute 'total_cost'
为什么?因为在我的mrp_extend对象中,我继承了mrp.bom并创建了新的total_cost。我做错了什么?请帮忙。
答案 0 :(得分:0)
功能字段的返回值应该是{<record_id>: <record functional field value>}
形式的字典。您可以从Here获取功能字段的详细信息。在函数def _get_total_cost(self, cr, uid, ids, field_name, arg, context):', the argument ids contain list of ids of the records for which the functional field is being calculated. So if you try to browse using the ids as
bom_point = self.pool.get('mrp.bom')。browse(cr,uid,ids,context = context)'中,您将获得浏览记录列表。因此,您必须遍历此浏览记录列表并返回结果。即;
def _get_total_cost(self, cr, uid, ids, field_name, arg, context):
res = {}
for bom_point in self.browse(cr, uid, ids, context=context):
total_cost = bom_point.bom_id.total_cost or 0.00
product_qty = bom_point.bom_id.product_qty or 0.00
total_cost = 0.00
if product_qty > 1:
total_cost = total_cost / product_qty
res[bom_point.id] = total_cost
return res
答案 1 :(得分:0)
bom_point的类型是记录列表,因此要浏览记录列表,必须定义元素。在您的情况下,您可以键入bom_point[0].total_cost
作为bom_point的第一条记录的total_cost。