Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

how to override write function

By
Dedi Sinaga
on 6/18/15, 2:56 AM 1,603 views

Hi, I use OE version 7.0 and I have a case like this.

In stock move, on module stock, we have write method defined

def write(self, cr, uid, ids, vals, context=None):
    if isinstance(ids, (int, long)):
        ids = [ids]
    if uid != 1:
        frozen_fields = set(['product_qty', 'product_uom', 'product_uos_qty', 'product_uos', 'location_id', 'location_dest_id', 'product_id'])
        for move in self.browse(cr, uid, ids, context=context):
            if move.state == 'done':
                if frozen_fields.intersection(vals):
                    raise osv.except_osv(_('Operation Forbidden!'),
_('Quantities, Units of Measure, Products and Locations cannot be modified on stock moves that have already been processed (except by the Administrator).'))
    return super(stock_move, self).write(cr, uid, ids, vals, context=context)


I want to override it in my custom module become like this one:

def write(self, cr, uid, ids, vals, context=None):
    if isinstance(ids, (int, long)):
        ids = [ids]
    access = self.pool.get('ir.model.access').check(cr, uid, 'stock.move', 'write', context)
    if uid != 1 and not access:
        frozen_fields = set(['product_qty', 'product_uom', 'product_uos_qty', 'product_uos', 'location_id', 'location_dest_id', 'product_id'])
        for move in self.browse(cr, uid, ids, context=context):
            if move.state == 'done':
                if frozen_fields.intersection(vals):
                    raise osv.except_osv(_('Operation Forbidden!'),
_('Quantities, Units of Measure, Products and Locations cannot be modified on stock moves that have already been processed (except by the Administrator).')) 
return super(stock_move, self).write(cr, uid, ids, vals, context=context)

If I use that method in my custom module, my method will be called but it keep raising exception from the first method too.

I need suggestions how to do it properly in OpenERP/ODOO

2

Baiju

--Baiju--
3154
| 5 4 8
Kochi, India
--Baiju--
ODOO technical and functional

        I like learning new thing, especially technical thing. I am a quick learner, I can learn technical things very fast. I have good logical skill,  I have immense interest in programming.

e-mail id : baijuks@hotmail.com

Baiju
On 6/18/15, 6:47 AM

Hi Sinaga,

To eliminate need of calling super method, you can use like this:-

instead of this -> super(stock_move, self).write(cr, uid, ids, vals, context=context)

you can give-> osv.osv.write(self, cr, uid, ids, vals, context=context)

This will call directly write function of ORM...


Hope this helps....


1
scot77844
On 6/18/15, 3:41 AM

Hi Dedi Sinaga,

You will be writing a class in the custom module like below.

class stock_move_inherit(osv.osv):

        _inherit = 'stock.move'

       def write(self, cr, uid, ids, vals, context=None):

if isinstance(ids, (int, long)):

ids = [ids]

access = self.pool.get('ir.model.access').check(cr, uid, 'stock.move', 'write', context)

if uid != 1 and not access:

frozen_fields = set(['product_qty', 'product_uom', 'product_uos_qty', 'product_uos', 'location_id', 'location_dest_id', 'product_id'])

for move in self.browse(cr, uid, ids, context=context):

if move.state == 'done':

if frozen_fields.intersection(vals):

raise osv.except_osv(_('Operation Forbidden!'),

_('Quantities, Units of Measure, Products and Locations cannot be modified on stock moves that have already been processed (except by the Administrator).'))

return super(stock_move_inherit, self).write(cr, uid, ids, vals, context=context)



In the return statement, the value should be your inherited class name (stock_move_inherit) but not the main class name (stock_move).

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 6/18/15, 2:56 AM
Seen: 1603 times
Last updated: 6/18/15, 6:48 AM