View and Model Inheritance
Inheritance
Model inheritance
Odoo provides two inheritance mechanisms to extend an existing model in a modular way.The first inheritance(Traditional Inheritance) mechanism allows a module to modify the behavior of a model defined in another module:
add fields to a model,
override the definition of fields on a model,
add constraints to a model,
add methods to a model,
override existing methods on a model.
The second inheritance mechanism (delegation) allows to link every record of a model to a record in a parent model, and provides transparent access to the fields of the parent record.
In OpenERP, there are 3 (4)ways to inherit from an existing model:
_inherit = 'model' (without any _name) ( Need to add fields in the same module)
_inherit = 'model' (with a specified _name) (Need to add fields in some other module )
_inherits = 'model' Polymorphic data using _inherits _inherits = {'res.partner': 'partner_id'}
So what about our '_name' property
If the _name as the same value as the inherited class it will do a basic inheritance.
If you forget to add the _inherit you will redefine the model
If your class _inherit one model and you set a _name different it will create a new model in a new database table.
If your class inherit many model you have to set _name if your override an existing model this way you may have some trouble, it should be avoided. It is better to use this to create new classes that inherit from abstract model.
What's the difference between them and, therefore, how to use them properly ?
In OpenERP we have many main type of inheritance:
Classical using Python inheritance.
It allows to add specific "generic" behavior to Model by inheriting classes that derive from orm.Model like geoModel that adds goegraphic support.
class Myclass(GeoModel, AUtilsClass):
Standard Using _inherit
The main objective is to add new behaviors/extend existing models. For example you want to add a new field to an invoice and add a new method: `
Multiple inheritance
_name = must be used
_inherit = ['model_1', 'model_2']
It is important to notice that _inherit can be a string or a list. You can do _inherit = ['model_1', 'model_2']
Polymorphic data using _inherits
_inherits = {'res.partner': 'partner_id'}
When using _inherits you will do a kind of polymorphic model in the database way.
This mean we create a model that gets the know how of a Model but adds aditional data/columns in a new database table. So when you create a user, all partner data is stored in res_partner table (and a partner is created) and all user related info is stored in res_users table.
To do this we use a dict:The key corresponds to the base model and the value to the foreign key to the base model.
View inheritance
Instead of modifying existing views in place (by overwriting them), Odoo provides view inheritance where children "extension" views are applied on top of root views, and can add or remove content from their parent.
An extension view references its parent using the inherit_id field, and instead of a single view its arch field is composed of any number of xpath elements selecting and altering the content of their parent view:
_inherit='res.company'
<record id="edit_res_company" model="ir.ui.view">
<field name="name">custom.module.form</field>
<field name="model">res.company</field> # Inherited Module
<field name="inherit_id" ref="base.view_company_form"/>
<!-- base=name of the module-->
<field name="arch" type="xml">
<!-- find field description and add the field
idea_ids after it -->
<field name="name" position="after">
<field name="domain_name"/> -->
OR
<xpath expr="//field[@name='description']" position="after">
<field name="idea_ids" string="Number of ideas"/>
</xpath>
</record>
expr
An XPath expression selecting a single element in the parent view. Raises an error if it matches no element or more than one
position
Operation to apply to the matched element:
inside
appends xpath's body at the end of the matched element
replace
replaces the matched element by the xpath's body
before
inserts the xpath's body as a sibling before the matched element
after
inserts the xpaths's body as a sibling after the matched element
attributes
alters the attributes of the matched element using special attribute elements in the xpath's body
Model inheritance
Odoo provides two inheritance mechanisms to extend an existing model in a modular way.The first inheritance(Traditional Inheritance) mechanism allows a module to modify the behavior of a model defined in another module:
add fields to a model,
override the definition of fields on a model,
add constraints to a model,
add methods to a model,
override existing methods on a model.
The second inheritance mechanism (delegation) allows to link every record of a model to a record in a parent model, and provides transparent access to the fields of the parent record.
In OpenERP, there are 3 (4)ways to inherit from an existing model:
_inherit = 'model' (without any _name) ( Need to add fields in the same module)
_inherit = 'model' (with a specified _name) (Need to add fields in some other module )
_inherits = 'model' Polymorphic data using _inherits _inherits = {'res.partner': 'partner_id'}
So what about our '_name' property
If the _name as the same value as the inherited class it will do a basic inheritance.
If you forget to add the _inherit you will redefine the model
If your class _inherit one model and you set a _name different it will create a new model in a new database table.
If your class inherit many model you have to set _name if your override an existing model this way you may have some trouble, it should be avoided. It is better to use this to create new classes that inherit from abstract model.
What's the difference between them and, therefore, how to use them properly ?
In OpenERP we have many main type of inheritance:
Classical using Python inheritance.
It allows to add specific "generic" behavior to Model by inheriting classes that derive from orm.Model like geoModel that adds goegraphic support.
class Myclass(GeoModel, AUtilsClass):
Standard Using _inherit
The main objective is to add new behaviors/extend existing models. For example you want to add a new field to an invoice and add a new method: `
Multiple inheritance
_name = must be used
_inherit = ['model_1', 'model_2']
It is important to notice that _inherit can be a string or a list. You can do _inherit = ['model_1', 'model_2']
Polymorphic data using _inherits
_inherits = {'res.partner': 'partner_id'}
When using _inherits you will do a kind of polymorphic model in the database way.
This mean we create a model that gets the know how of a Model but adds aditional data/columns in a new database table. So when you create a user, all partner data is stored in res_partner table (and a partner is created) and all user related info is stored in res_users table.
To do this we use a dict:The key corresponds to the base model and the value to the foreign key to the base model.
View inheritance
Instead of modifying existing views in place (by overwriting them), Odoo provides view inheritance where children "extension" views are applied on top of root views, and can add or remove content from their parent.
An extension view references its parent using the inherit_id field, and instead of a single view its arch field is composed of any number of xpath elements selecting and altering the content of their parent view:
_inherit='res.company'
<record id="edit_res_company" model="ir.ui.view">
<field name="name">custom.module.form</field>
<field name="model">res.company</field> # Inherited Module
<field name="inherit_id" ref="base.view_company_form"/>
<!-- base=name of the module-->
<field name="arch" type="xml">
<!-- find field description and add the field
idea_ids after it -->
<field name="name" position="after">
<field name="domain_name"/> -->
OR
<xpath expr="//field[@name='description']" position="after">
<field name="idea_ids" string="Number of ideas"/>
</xpath>
</record>
expr
An XPath expression selecting a single element in the parent view. Raises an error if it matches no element or more than one
position
Operation to apply to the matched element:
inside
appends xpath's body at the end of the matched element
replace
replaces the matched element by the xpath's body
before
inserts the xpath's body as a sibling before the matched element
after
inserts the xpaths's body as a sibling after the matched element
attributes
alters the attributes of the matched element using special attribute elements in the xpath's body
0 comments:
Post a Comment