- How do we extend an existing Model to add a mixin, such as
mail.thread
?
Both in-place classic extension and mixin prototype inheritance use the same _inherit
Model attribute. For example, to extend x
and add the y
mixin, we should have the _name='x'
model attribute and the _inherit = ['x', 'y']
Model attribute.
- What changes are needed to also have the phone field available in the member form view?
No model changes are needed, since a phone
field exists in partners, and delegation inheritance makes all partner fields also available on the member Model. We only need to add the field to the form view, for example, next to the email field: <field name=phone" />
.
- What happens if you create a Model class with name attribute different form the inherit attribute (for example,
_name='y'
and_inherit='x'
)?
A new y
model will be created, copying all the features (data structure and methods) from Model x
. If x
is a database-stored Model, we will have two database tables with the same fields, but independent data records, which is not a good design practice. That's why this is usually used with abstract mixin classes that don't have an actual database representation.
- Can XPath be used to modify data record from other modules?
No. XPath is used only for views, to find the element in the view structure where the extension should be done. This extension is performed at runtime, and no actual change is made to the record being extended.
- When inheriting a Model to extend it, can we extend a method and not use
super()
to call original code being inherited?
We can, and sometimes we may have to, but we should avoid it. When not using super()
we are completely replacing the original code, our re-implementation will have different behavior form the original one, and it may have unanticipated side-effects. Also, extensions implemented in other modules may not work as expected. If the parent method is hard to extend, it's best to propose changes to it, so that it includes extension points we can use (separating part of the logic into their own methods will make extension simpler).
- How can we extend the book catalog web page to add the ISBN field at the end of the line without referencing any particular field name?
We can insert the ISBN field inside the row div
element: <xpath expr="//div[class='row']"><span t-field="book.isbn"/></xpath>