Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
2 Odpovědi
629 Zobrazení

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
Avatar
Zrušit
Nejlepší odpověď

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.

Avatar
Zrušit
Nejlepší odpověď

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'):
...




Avatar
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
2
pro 22
12190
0
bře 15
4744
2
čvn 21
5666
1
zář 24
4321
2
čvn 15
9252