9
.. i18n: Traditional Inheritance
10
.. i18n: -----------------------
13
Traditional Inheritance
14
-----------------------
23
.. i18n: Objects may be inherited in some custom or specific modules. It is better to inherit an object to add/modify some fields.
26
Objects may be inherited in some custom or specific modules. It is better to inherit an object to add/modify some fields.
28
.. i18n: It is done with::
30
.. i18n: _inherit='object.name'
32
.. i18n: Extension of an object
33
.. i18n: ++++++++++++++++++++++
38
_inherit='object.name'
40
Extension of an object
41
++++++++++++++++++++++
43
.. i18n: There are two possible ways to do this kind of inheritance. Both ways result in a new class of data, which holds parent fields and behaviour as well as additional fielda and behaviour, but they differ in heavy programatical consequences.
46
There are two possible ways to do this kind of inheritance. Both ways result in a new class of data, which holds parent fields and behaviour as well as additional fielda and behaviour, but they differ in heavy programatical consequences.
48
.. i18n: While Example 1 creates a new subclass "custom_material" that may be "seen" or "used" by any view or tree which handles "network.material", this will not be the case for Example 2.
51
While Example 1 creates a new subclass "custom_material" that may be "seen" or "used" by any view or tree which handles "network.material", this will not be the case for Example 2.
53
.. i18n: This is due to the table (other.material) the new subclass is operating on, which will never be recognized by previous "network.material" views or trees.
56
This is due to the table (other.material) the new subclass is operating on, which will never be recognized by previous "network.material" views or trees.
60
.. i18n: class custom_material(osv.osv):
61
.. i18n: _name = 'network.material'
62
.. i18n: _inherit = 'network.material'
64
.. i18n: 'manuf_warranty': fields.boolean('Manufacturer warranty?'),
66
.. i18n: _defaults = {
67
.. i18n: 'manuf_warranty': lambda *a: False,
69
.. i18n: custom_material()
74
class custom_material(osv.osv):
75
_name = 'network.material'
76
_inherit = 'network.material'
78
'manuf_warranty': fields.boolean('Manufacturer warranty?'),
81
'manuf_warranty': lambda *a: False,
85
.. i18n: .. tip:: Notice
87
.. i18n: _name == _inherit
94
.. i18n: In this example, the 'custom_material' will add a new field 'manuf_warranty' to the object 'network.material'. New instances of this class will be visible by views or trees operating on the superclasses table 'network.material'.
97
In this example, the 'custom_material' will add a new field 'manuf_warranty' to the object 'network.material'. New instances of this class will be visible by views or trees operating on the superclasses table 'network.material'.
99
.. i18n: This inheritancy is usually called "class inheritance" in Object oriented design. The child inherits data (fields) and behavior (functions) of his parent.
102
This inheritancy is usually called "class inheritance" in Object oriented design. The child inherits data (fields) and behavior (functions) of his parent.
106
.. i18n: class other_material(osv.osv):
107
.. i18n: _name = 'other.material'
108
.. i18n: _inherit = 'network.material'
109
.. i18n: _columns = {
110
.. i18n: 'manuf_warranty': fields.boolean('Manufacturer warranty?'),
112
.. i18n: _defaults = {
113
.. i18n: 'manuf_warranty': lambda *a: False,
115
.. i18n: other_material()
120
class other_material(osv.osv):
121
_name = 'other.material'
122
_inherit = 'network.material'
124
'manuf_warranty': fields.boolean('Manufacturer warranty?'),
127
'manuf_warranty': lambda *a: False,
131
.. i18n: .. tip:: Notice
133
.. i18n: _name != _inherit
140
.. i18n: In this example, the 'other_material' will hold all fields specified by 'network.material' and it will additionally hold a new field 'manuf_warranty'. All those fields will be part of the table 'other.material'. New instances of this class will therefore never been seen by views or trees operating on the superclasses table 'network.material'.
143
In this example, the 'other_material' will hold all fields specified by 'network.material' and it will additionally hold a new field 'manuf_warranty'. All those fields will be part of the table 'other.material'. New instances of this class will therefore never been seen by views or trees operating on the superclasses table 'network.material'.
145
.. i18n: This type of inheritancy is known as "inheritance by prototyping" (e.g. Javascript), because the newly created subclass "copies" all fields from the specified superclass (prototype). The child inherits data (fields) and behavior (functions) of his parent.
148
This type of inheritancy is known as "inheritance by prototyping" (e.g. Javascript), because the newly created subclass "copies" all fields from the specified superclass (prototype). The child inherits data (fields) and behavior (functions) of his parent.
150
.. i18n: Inheritance by Delegation
151
.. i18n: -------------------------
154
Inheritance by Delegation
155
-------------------------
157
.. i18n: **Syntax :**::
159
.. i18n: class tiny_object(osv.osv)
160
.. i18n: _name = 'tiny.object'
161
.. i18n: _table = 'tiny_object'
162
.. i18n: _inherits = { 'tiny.object_a' : 'name_col_a', 'tiny.object_b' : 'name_col_b',
163
.. i18n: ..., 'tiny.object_n' : 'name_col_n' }
169
class tiny_object(osv.osv)
170
_name = 'tiny.object'
171
_table = 'tiny_object'
172
_inherits = { 'tiny.object_a' : 'name_col_a', 'tiny.object_b' : 'name_col_b',
173
..., 'tiny.object_n' : 'name_col_n' }
176
.. i18n: The object 'tiny.object' inherits from all the columns and all the methods from the n objects 'tiny.object_a', ..., 'tiny.object_n'.
179
The object 'tiny.object' inherits from all the columns and all the methods from the n objects 'tiny.object_a', ..., 'tiny.object_n'.
181
.. i18n: To inherit from multiple tables, the technique consists in adding one column to the table tiny_object per inherited object. This column will store a foreign key (an id from another table). The values *'name_col_a' 'name_col_b' ... 'name_col_n'* are of type string and determine the title of the columns in which the foreign keys from 'tiny.object_a', ..., 'tiny.object_n' are stored.
184
To inherit from multiple tables, the technique consists in adding one column to the table tiny_object per inherited object. This column will store a foreign key (an id from another table). The values *'name_col_a' 'name_col_b' ... 'name_col_n'* are of type string and determine the title of the columns in which the foreign keys from 'tiny.object_a', ..., 'tiny.object_n' are stored.
186
.. i18n: This inheritance mechanism is usually called " *instance inheritance* " or " *value inheritance* ". A resource (instance) has the VALUES of its parents.
189
This inheritance mechanism is usually called " *instance inheritance* " or " *value inheritance* ". A resource (instance) has the VALUES of its parents.