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

i odoo mrp i have custom addons that inherit the mrp production and add some logic to the action_confirm button as follow:

def action_confirm(self):
res = super(MrpProduction, self).action_confirm()
for rec in self:
if not rec.division_line:
raise ValidationError(_("Add Division"))
if not rec.location_confirm:
raise ValidationError("Confirm Location ")
​return res
# In other custom addons i need to inherit the same function and modify this if condition, i added the custom module in the depends in my other custom module and then call the same function and modify the if statement but still the raise executed from the first inherited function
Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,


In Odoo, multiple modules override the same method (action_confirm), but Odoo only executes the method from the last-loaded module unless they are chained properly using super().


In your case, the second custom module overrides action_confirm, but the super() call refers only to mrp.production, not to the previously customized version in your first module.


Make sure both custom modules call super() correctly, so Odoo can chain all overrides.


Module 1


class MrpProduction(models.Model):
_inherit = 'mrp.production'

def action_confirm(self):
res = super().action_confirm()
for rec in self:
if not rec.division_line:
raise ValidationError(_("Add Division"))
if not rec.location_confirm:
raise ValidationError("Confirm Location")
return res


Module 2


class MrpProduction(models.Model):
_inherit = 'mrp.production'

def action_confirm(self):
res = super().action_confirm()
# You can add your conditions here
return res



In Module 2, add Module 1 in the depends list in the __manifest__.py.--


Hope it helps.

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

Multiple approaches, depends on what you need to do. In your other module, you could do the super() call in an else condition to execute only if your conditions to process differently are not met:

def action_confirm(self):
​ if your_condition:
for rec in self:
​ self.do_your_stuff()
​ else:
return super(MrpProduction, self).action_confirm()

Or, for example, apply a context that can be checked where/whenever needed:

# Module A
def action_confirm(self):
self = self.with_context(some_speaking_key=True)
res = super(MrpProduction, self).action_confirm()
for rec in self:
if not rec.division_line:
raise ValidationError(_("Add Division"))
if not rec.location_confirm:
raise ValidationError("Confirm Location ")
​return res

# Module B
def action_confirm(self):
if self._context.get('some_speaking_key'):
...




Ả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 22
11895
0
thg 3 15
4535
2
thg 6 21
5488
1
thg 9 24
4113
2
thg 6 15
9074