Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
5 ตอบกลับ
5436 มุมมอง

can you please tell me how to override the force_assign method in stock module, in model stock.picking. Especially I want to know how to use the @api.returns  decorator in the case of overriding the force_assign method. force_assign is a small method in stock module of object stock.picking. I am using odoo v9. In case, you need I have copied the code of force_assign here. 

The overriding method should just simply raise a UserError:

def force_assign(self,cr,uid,ids,context=context)
"""changes state to assigned
@return:True
"""
res=self.write(cr,uid,ids,{'state':'assigned'},context=context)
self.check_recompute_pack_op (cr,uid,ids,context=context)
return res
อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hello Raiye hailu, 


You need to use super keyword for override any existing method:
EX:-
def force_assign(self,cr,uid,ids,context=context)
     // Put your code
     return super(model_name, self).force_assign(cr, uid, ids, context=context)
Hope it will works for you.
Thanks,

อวตาร
ละทิ้ง
ผู้เขียน

Dear Jignesh, thank you for your response. But it did not work.Odoo simply ignores the method.

ผู้เขียน

Dear jignesh. Actually your suggestion works. I had forgotten to put the raise keyword before calling exceptions.UserError

คำตอบที่ดีที่สุด

You can easily Override "force_assign" method as follows (using new api).

@api.multi def force_assign(self):

 "Put Your Warning Here"

-> @api.returns is used when your "return value" is an Object.

i.e @api.returns(self) is written when method returns its own object(self).


อวตาร
ละทิ้ง
ผู้เขียน

Dear Jainesh your solution partially works. The force_assign button no longer changes the state of the record But it is not showing me the error message. My code right now is.

from openerp import exceptions

@api.multi

def force_assign (self):

exceptions.UserError ('error')

What are we missing?

ผู้เขียน

I have found out what the code is missing , it is missing the raise keyword before exceptions.UserError statement. Thank you Jainesh for the response.

ผู้เขียน คำตอบที่ดีที่สุด

Thank you guys . I have found that both solutions work. I had forgotten to put the raise keyword before 

calling exceptions.UserError.

Therefore the correct code is:

#new api
@api.multi
    def force_assign(self):
        raise exceptions.UserError('Error') 
#old api
def force_assign(self,cr,uid,ids,context=None)
     raise exceptions.UserError ('Error')
     return super(model_name, self).force_assign(cr, uid, ids, context=context)#this line is not necessarily needed
อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
Create function problem แก้ไขแล้ว
2
มิ.ย. 23
2337
1
พ.ย. 21
6206
0
ก.ค. 24
4676
Overriding method in a model แก้ไขแล้ว
1
มิ.ย. 20
35378
0
ต.ค. 19
3380