Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
4734 Lượt xem

Here's the write () method i want to restrict in my custom module. I don't want this existing write method to work how can I do that? 

class Product (models.Model):

    _name = 'product.product'


     @ api multi

def  write ( self ,  values ): 
'' 'Store the standard price change in order to be able to retrieve the cost of a product for a given date' '' 
res = super ( ProductProduct , self ). write ( values )
if 'standard_price' in values :
    self . _set_standard_price ( values [ 'standard_price' ])
return res
I can't comment this code as it belongs to the source model. I just want this function to bypass so it wont have any effect because i have written another method according to my requirement.



Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

If you want to overwrite a method on a model you should define the function without any super calls in a file that inherits the model where the function you want to overwrite exists.

Ảnh đại diện
Huỷ bỏ
Tác giả

I have tried to completely override the write() method in my inherited model but it doesn't update the average cost on product. But when i commented these lines

if 'standard_price' in values :

self . _set_standard_price ( values [ 'standard_price' ])

from the above code then it worked as required. But obviously i cant comment the source code but i just want this to not do anything it should be as useless as when i comment the code. how can I do that?

Tác giả Câu trả lời hay nhất

This is the solution I found from forum which worked for me. You can follow below code in order to achieve this part.

class CustomModel(models.Model):
    _inherit = "custom.model"
    
    @api.multi
    def write(self, vals):
        ## Definition
        return super(models.Model, self).write(vals)

Here only this function and write function of "model.Model" will call.

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 12 19
3387
2
thg 7 24
2496
1
thg 6 24
4986
1
thg 10 23
10674
1
thg 10 23
98