9. Extend modules without breaking them
Objectives
- extend a core model with PoolMeta
- preserve cooperative inheritance
- modify views through stable XML references
Mental model
The Pool composes classes from installed modules. Your extension participates in that composition; it does not own the whole model or its method chain.
Guided practice
- Declare the owner module as a dependency.
- Use PoolMeta and the exact technical model name.
- Call super with compatible arguments and return semantics.
- Add only the smallest field or rule needed.
- Test with other modules that extend the same method.
Minimal example
from trytond.model import fields
from trytond.pool import PoolMeta
class Party(metaclass=PoolMeta):
__name__ = 'party.party'
favorite_author = fields.Many2One(
'library.author', "Favorite Author")
@classmethod
def create(cls, vlist):
parties = super().create(vlist)
# Post-process only what this module owns.
return parties
# __init__.py
Pool.register(Party, module='library', type_='model')
How to read the example
Never copy a core class into your module to change one method. That freezes old behavior and bypasses other extensions. XML inheritance should target stable IDs and narrow XPath expressions.
Exercise
Extend party.party, add one field to its form, install another party extension, and prove both method chains execute.
Run the exercise first with a minimal valid case, then add an invalid case and turn both into repeatable tests.
Verifiable result
The extension disappears cleanly when the module is not activated and coexists with other installed extensions.
Common mistakes
- omitting super
- changing a public signature
- depending on installation order instead of dependencies
- replacing a complete core view
Ready-to-advance criteria
- I can explain the concepts without looking at the code.
- Valid and invalid cases have tests.
- I tested with a non-administrative user.
- I know how to upgrade and restore the training database.
Version and references
Series 7.0 requires at least Python 3.8 according to official metadata. This official branch still carries historical metadata in setup.py; preserve series compatibility while preparing custom packages for standard python -m build builds. Do not confuse this historical minimum with the Python version certified by your organization.
This edition pins examples and dependencies to series 7.0. Check the official 7.0 tutorial, server API, and source branch before moving a pattern to production.