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

The Truble Happening when i was working with @api.constaint


i want to check whether current situation is Create or Update.. I want to Do the Action only for Update Method. Not For Create.  The @api.contstraint will work on Create too. How i check it. whether current context is Create or Update

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

Hi

   The function which work when a model is updated is write you can use it for your validation.

@api.multi
def write(self, values):
res = super(ClassName, self).write(values)
#
your code
return res

For create use def create

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

According the documentation the @api.constrains will invoke the method decorated everytime the record gets modified so it will be invoked on create , update or any method who modify the data.

ref: https://www.odoo.com/documentation/9.0/reference/orm.html#openerp.api.constrains

So, you can override the update method and add your validation at the start of the method, you have to write the code in the same py file of your class, if you're using a stock model, i suggest you create a model who inherits from the stock model:


def method_name(self):

     #DO THE VALIDATION

     if (validation== True):

         #DO THE ACTION

When you override a method, the new method will only work in that way for that model.

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 4 16
3758
1
thg 8 25
980
1
thg 8 25
1219
2
thg 7 25
3962
3
thg 7 25
1283