In OpenERP, there are 3 ways to inherit from an existing model:
_inherit = 'model'(without any_name)_inherit = 'model'(with a specified_name)_inherits = 'model'
What's the difference between them and, therefore, how to use them properly ?
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
In OpenERP, there are 3 ways to inherit from an existing model:
_inherit = 'model' (without any _name)_inherit = 'model' (with a specified _name)_inherits = 'model'What's the difference between them and, therefore, how to use them properly ?
This is a wide question:
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: `
class AccountInvoice(orm.Model):
_inherit = "account.invoice"
_column = {'my_field': fields.char('My new field')}
def a_new_func(self, cr, uid, ids, x, y, context=None):
# my stuff
return something
`
You can also override existing methods:`
def existing(self, cr, uid, ids, x, y, z, context=None):
parent_res = super(AccountInvoice, self).existing(cr, uid, ids, x, y, z, context=context)
# my stuff
return parent_res_plus_my_stuff`
It is important to note that the order of the super call is defined by the inheritance graph of the addons (the depends key in __openerp__.py).
It is important to notice that _inherit can be a string or a list. You can do _inherit = ['model_1', 'model_2'].
List allows to create a class that concatenate multiple Model, TransientModel or better AbstractModel into a single new model.
So what about our '_name' property
_inherit you will redefine the model_name different it will create a new model in a new database table._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.Polymorphic data using _inherits
When using _inherits you will do a kind of polymorphic model in the database way.
For example product.product inherits product.template or res.users inherits res.partner. 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:
_inherits = {'res.partner': 'partner_id'}
The key corresponds to the base model and the value to the foreign key to the base model.
From here you can mix inheritance if you dare...
Hope it helps.
What will happen when using _inherits we use the same _name?
which is used mostly in OpenERP? which do you recommend?
Python default override class declaration, then how its works here ? Like product and stock module have class product declaration, where it do not overwrite previously declared class method and features ? How it keeps methods declared in other modules ?
This will help you to learn Different Types of Inheritance...
http://net-informations.com/faq/oops/inheritancetype.htm
Create an account today to enjoy exclusive features and engage with our awesome community!
Реєстрація| Related Posts | Відповіді | Переглядів | Дія | |
|---|---|---|---|---|
|
|
1
жовт. 24
|
2297 | ||
|
|
1
бер. 15
|
11086 | ||
|
|
1
квіт. 24
|
2661 | ||
|
|
5
серп. 23
|
58425 | ||
|
|
7
груд. 23
|
26912 |
here is step by step guide about odoo inheritance: https://learnopenerp.blogspot.com/2018/01/inheritance-in-models-and-views.html