8
.. i18n: Traditional Inheritance
9
.. i18n: -----------------------
11
Traditional Inheritance
12
-----------------------
20
.. i18n: Objects may be inherited in some custom or specific modules. It is better to inherit an object to add/modify some fields.
22
Objects may be inherited in some custom or specific modules. It is better to inherit an object to add/modify some fields.
24
.. i18n: It is done with::
26
.. i18n: _inherit='object.name'
28
.. i18n: Extension of an object
29
.. i18n: ++++++++++++++++++++++
33
_inherit='object.name'
35
Extension of an object
36
++++++++++++++++++++++
38
.. 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.
40
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.
42
.. 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.
44
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.
46
.. 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.
48
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.
52
.. i18n: class custom_material(osv.osv):
53
.. i18n: _name = 'network.material'
54
.. i18n: _inherit = 'network.material'
56
.. i18n: 'manuf_warranty': fields.boolean('Manufacturer warranty?'),
58
.. i18n: _defaults = {
59
.. i18n: 'manuf_warranty': lambda *a: False,
61
.. i18n: custom_material()
65
class custom_material(osv.osv):
66
_name = 'network.material'
67
_inherit = 'network.material'
69
'manuf_warranty': fields.boolean('Manufacturer warranty?'),
72
'manuf_warranty': lambda *a: False,
76
.. i18n: .. tip:: Notice
78
.. i18n: _name == _inherit
84
.. 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'.
86
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'.
88
.. i18n: This inheritancy is usually called "class inheritance" in Object oriented design. The child inherits data (fields) and behavior (functions) of his parent.
90
This inheritancy is usually called "class inheritance" in Object oriented design. The child inherits data (fields) and behavior (functions) of his parent.
94
.. i18n: class other_material(osv.osv):
95
.. i18n: _name = 'other.material'
96
.. i18n: _inherit = 'network.material'
98
.. i18n: 'manuf_warranty': fields.boolean('Manufacturer warranty?'),
100
.. i18n: _defaults = {
101
.. i18n: 'manuf_warranty': lambda *a: False,
103
.. i18n: other_material()
107
class other_material(osv.osv):
108
_name = 'other.material'
109
_inherit = 'network.material'
111
'manuf_warranty': fields.boolean('Manufacturer warranty?'),
114
'manuf_warranty': lambda *a: False,
118
.. i18n: .. tip:: Notice
120
.. i18n: _name != _inherit
126
.. 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'.
128
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'.
130
.. 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.
132
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.
134
.. i18n: Inheritance by Delegation
135
.. i18n: -------------------------
137
Inheritance by Delegation
138
-------------------------
140
.. i18n: **Syntax :**::
142
.. i18n: class tiny_object(osv.osv)
143
.. i18n: _name = 'tiny.object'
144
.. i18n: _table = 'tiny_object'
145
.. i18n: _inherits = { 'tiny.object'_1_ : name_col'_1_', 'tiny.object'_2_ : name_col'_2_', ..., 'tiny.object'_n_ : name_col'_n_' }
150
class tiny_object(osv.osv)
151
_name = 'tiny.object'
152
_table = 'tiny_object'
153
_inherits = { 'tiny.object'_1_ : name_col'_1_', 'tiny.object'_2_ : name_col'_2_', ..., 'tiny.object'_n_ : name_col'_n_' }
156
.. i18n: The object 'tiny.object' inherits from all the columns and all the methods from the n objects 'tiny.object'_1_, ..., 'tiny.object'_n_.
158
The object 'tiny.object' inherits from all the columns and all the methods from the n objects 'tiny.object'_1_, ..., 'tiny.object'_n_.
160
.. 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'_1_' name_col'_2_' ... name_col'_n_'* are of type string and determine the title of the columns in which the foreign keys from 'tiny.object'_1_, ..., 'tiny.object'_n_ are stored.
162
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'_1_' name_col'_2_' ... name_col'_n_'* are of type string and determine the title of the columns in which the foreign keys from 'tiny.object'_1_, ..., 'tiny.object'_n_ are stored.
164
.. i18n: This inheritance mechanism is usually called " *instance inheritance* " or " *value inheritance* ". A resource (instance) has the VALUES of its parents.
166
This inheritance mechanism is usually called " *instance inheritance* " or " *value inheritance* ". A resource (instance) has the VALUES of its parents.