如何修改原class里的方法



  • 按照Odoo的风格,理论上:
    Class A中的方法可以由继承A的ClassB中被修改  ?

    问题如下:

    在stock.py中的:

    [quote]
    class stock_picking(models.Model):
        _name = "stock.picking"
        _inherit = ['mail.thread']
        _description = "Transfer"
        _order = "priority desc, date asc, id desc"

        def _set_min_date(.....
    [/quote]



    [quote]


        def create_lots_for_picking(self, cr, uid, ids, context=None):
            lot_obj = self.pool['stock.production.lot']
            opslot_obj = self.pool['stock.pack.operation.lot']
            to_unlink = []
            for picking in self.browse(cr, uid, ids, context=context):
                for ops in picking.pack_operation_ids:
                    for opslot in ops.pack_lot_ids:
                        if not opslot.lot_id:
                            print '=============not find stock.production.lot, create lot:',opslot.lot_name,ops.product_id.id,opslot.lot_weight
                            lot_id = lot_obj.create(cr, uid, {'name': opslot.lot_name, 'product_id': ops.product_id.id}, context=context)
                         
                            opslot_obj.write(cr, uid, [opslot.id], {'lot_id':lot_id}, context=context)
                    #Unlink pack operations where qty = 0
                    to_unlink += [x.id for x in ops.pack_lot_ids if x.qty == 0.0]
            opslot_obj.unlink(cr, uid, to_unlink, context=context)

    [/quote]

    现在需要修改这个方法,新建模块后:

    [quote]

    class dy_stock_picking(models.Model):
        inherit = 'stock.picking'

        def create_lots_for_picking(self, cr, uid, ids, context=None):
            print 'dy.stock.picking==============='
            lot_obj = self.pool['stock.production.lot']
            opslot_obj = self.pool['stock.pack.operation.lot']
            to_unlink = []
            for picking in self.browse(cr, uid, ids, context=context):
                for ops in picking.pack_operation_ids:
                    for opslot in ops.pack_lot_ids:
                        if not opslot.lot_id:
                            print 'dy.stock.picking=============not find stock.production.lot, create lot:',opslot.lot_name,ops.product_id.id
                            lot_id = lot_obj.create(cr, uid, {'name': opslot.lot_name, 'product_id': ops.product_id.id, 'lot_weight': opslot.lot_weight}, context=context)
                            opslot_obj.write(cr, uid, [opslot.id], {'lot_id':lot_id}, context=context)
                    #Unlink pack operations where qty = 0
                    to_unlink += [x.id for x in ops.pack_lot_ids if x.qty == 0.0]
            opslot_obj.unlink(cr, uid, to_unlink, context=context)

    [/quote]

    运行后发现,没有成功:
    [img]http://shine-it.net/index.php?action=dlattach;topic=17670.0;attach=5160;image[/img]
    还是调用了原来的方法
    什么原因?



  • 按照Odoo的风格,理论上:
    Class A中的方法可以由继承A的ClassB中被修改  ?

    问题如下:

    在stock.py中的:

    [quote]
    class stock_picking(models.Model):
        _name = "stock.picking"
        _inherit = ['mail.thread']
        _description = "Transfer"
        _order = "priority desc, date asc, id desc"

        def _set_min_date(.....
    [/quote]



    [quote]


        def create_lots_for_picking(self, cr, uid, ids, context=None):
            lot_obj = self.pool['stock.production.lot']
            opslot_obj = self.pool['stock.pack.operation.lot']
            to_unlink = []
            for picking in self.browse(cr, uid, ids, context=context):
                for ops in picking.pack_operation_ids:
                    for opslot in ops.pack_lot_ids:
                        if not opslot.lot_id:
                            print '=============not find stock.production.lot, create lot:',opslot.lot_name,ops.product_id.id,opslot.lot_weight
                            lot_id = lot_obj.create(cr, uid, {'name': opslot.lot_name, 'product_id': ops.product_id.id}, context=context)
                         
                            opslot_obj.write(cr, uid, [opslot.id], {'lot_id':lot_id}, context=context)
                    #Unlink pack operations where qty = 0
                    to_unlink += [x.id for x in ops.pack_lot_ids if x.qty == 0.0]
            opslot_obj.unlink(cr, uid, to_unlink, context=context)

    [/quote]

    现在需要修改这个方法,新建模块后:

    [quote]

    class dy_stock_picking(models.Model):
        inherit = 'stock.picking'

        def create_lots_for_picking(self, cr, uid, ids, context=None):
            print 'dy.stock.picking==============='
            lot_obj = self.pool['stock.production.lot']
            opslot_obj = self.pool['stock.pack.operation.lot']
            to_unlink = []
            for picking in self.browse(cr, uid, ids, context=context):
                for ops in picking.pack_operation_ids:
                    for opslot in ops.pack_lot_ids:
                        if not opslot.lot_id:
                            print 'dy.stock.picking=============not find stock.production.lot, create lot:',opslot.lot_name,ops.product_id.id
                            lot_id = lot_obj.create(cr, uid, {'name': opslot.lot_name, 'product_id': ops.product_id.id, 'lot_weight': opslot.lot_weight}, context=context)
                            opslot_obj.write(cr, uid, [opslot.id], {'lot_id':lot_id}, context=context)
                    #Unlink pack operations where qty = 0
                    to_unlink += [x.id for x in ops.pack_lot_ids if x.qty == 0.0]
            opslot_obj.unlink(cr, uid, to_unlink, context=context)

    [/quote]

    运行后发现,没有成功:
    [img]http://shine-it.net/index.php?action=dlattach;topic=17670.0;attach=5160;image[/img]
    还是调用了原来的方法
    什么原因?



  • 你把inherit = 'stock.picking' 修改成_inherit = 'stock.picking'试试



  • [quote author=青岛-随风 link=topic=17670.msg32443#msg32443 date=1468488224]
    你把inherit = 'stock.picking' 修改成_inherit = 'stock.picking'试试
    [/quote]


    你太对了,谢谢谢谢


登录后回复
 

与 Odoo 中文社区 的连接断开,我们正在尝试重连,请耐心等待