Best practice to keep code changes maintainable when overriding methods in Odoo

62 Views Asked by At

I've been maintaining and extending an Odoo installation. Often I need to override or monkeypatch certain methods (that can't be inherited) in order to achieve the desired result.

What do you think is the best way to keep the changes maintainable?

So far there are two ways the changes are pushed in production:

  1. Creating a new module and overriding the method completely in a separate repo -> pushing+deploying git repo to production.
  2. Changing only the the code needed in local Odoo git repo, committing -> pushing+deploying git repo to production.

The problem rises when upstream code changes and breaks the override (either when minor changes happen upstream or major version upgrades).

In the first case if the method signature is the same the overridden method keeps getting called even if the content of the original method changed completely.

In the second case if there are major changes the merge/rebase fails when pulling from upstream.

Lastly there are cases when other in-house modules depend on the overridden method to function properly.

1

There are 1 best solutions below

0
Alesis Joan On

Never never do monkey patch (except for tests). Always try to call super with an if condition or putting your code after it.

Remember that stack trace is built in odoo by module sequences (see manifiest file).

Our structure is in one repo:

  • external addons: those addons like OCA
  • inhouse addonds: our customizations addons. Sometimes our inhouse addons extends for external or core/enterprise.
  • odoo addons: core+enterprise.

Our standard is:

  • If one of the external addons changes, we manually add those changes.
  • Our customizations have high sequence, meaning they will be consider at the top of the stack trace.

Hint: CD/CI is made by one addons 'test' that depends on the addons installed on prod. So just marking that addon to be tested, tests are triggered with our desired setup.