Henry Lim Henry Lim - 4 years ago 81
Python Question

(Python Code) Can't call my new field in new object which extend from mrp.bom in OpenERP v7?

Below is my coding which I have create:

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)]})
#-----------------------------
}


I have new create 2 object. One is mrp_extend and this object is inherit from mrp.bom.

Then I have create new object name mrp_production_extend and this object is inherit from mrp.production.

Now my problem is, I have create a function in 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


but this function no work and show me this error:

AttributeError: 'browse_record_list' object has no attribute 'total_cost'


WHY? because in my mrp_extend object I have inherit to mrp.bom and create new total_cost. Anything I do wrong? Please help.

Answer Source

The return value of a functional field should be a dictionary of the form {<record_id>: <record functional field value>}. You can get the details of functional fields from Here. In your function 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 asbom_point = self.pool.get('mrp.bom').browse(cr, uid, ids, context=context)', you will get a list of browse records. So you have to loop through this browse record list and return your result. ie;

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download