2

以下は、私が作成したコーディングです。

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

私は新しい作成2オブジェクトを持っています。1 つは 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 を作成しているためです。私が間違っていることはありますか?助けてください。

4

2 に答える 2

0

機能フィールドの戻り値は、形式の辞書でなければなりません{<record_id>: <record functional field value>}。機能フィールドの詳細は、ここから取得できます。関数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)' で、ブラウズ レコードのリストを取得します。したがって、このブラウズ レコード リストをループして、結果を返す必要があります。すなわち;

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
于 2013-11-07T10:42:53.987 に答える