Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
2 Risposte
889 Visualizzazioni

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
Abbandona
Risposta migliore

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
Abbandona
Risposta migliore

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
Abbandona
Post correlati Risposte Visualizzazioni Attività
2
dic 22
12470
0
lug 25
4950
2
giu 21
5845
1
set 24
4550
2
giu 15
9480