Inheritance in OOP – Openerp (ODOO)

I am new to openERP and accepted an interview. Please explain the idea of ​​different types of inheritance in openERP. I think there are 3 types in total. Please explain in a very simple way from the perspective of the interview.
PS: I am familiar with the concept of simple inheritance.
Inheritance:

The inheritance mechanism is used to create the concept of reusability. Reusability means to reuse the code of the parent class in any object-oriented programming.

Benefits:

>Reduce code redundancy .
>Provides code reusability.
>Reduces source code size and improves code readability.
>Code is easy to manage and is divided into parent and child classes.
>By overriding the base class To support code scalability
functions in subclasses.

Disadvantages:

>In inheritance, the base class and the subclass are tightly coupled.
Therefore, if You change the code of the parent class, it will be affected
all child classes.
>In the class hierarchy, many data members remain unused and memory
assigned to them is not utilized. So it affects you
Program, if you don’t implement inheritance correctly.

There are two ways of inheritance in OpenERP.

1. Classic Pythonic way:

It allows to add specific “generic” behaviors to the Model by inheriting classes derived from orm.Model (such as geoModel with goegraphic support).

class Myclass(GeoModel, AUtilsClass):

Use _inherit: –

The main goal is to add new behaviors/extend existing models. For example, you want to add new fields to invoices and add new methods

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

Overwrite the existing method:

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

2.Polymorphic way: –

Use _inherits: –

When using _inherits, you will execute a polymorphic model in a database way.

For example, product.product inherits product.template or res.users inherits res.partner. This means that we have created a model that can obtain knowledge of the model, but adds additional data/columns in the new database table. Therefore, When you create a user, all partner data is stored in the res_partner table (and partners are created), and all user-related information is stored in the res_users table.

For this, we use dict :_Inherits = {‘res.partner’:’partner_id’} This key corresponds to the basic model and the foreign key value of the basic model.

You can also inherit Odoo views (such as form views, tree views) through XML , Search view, etc.), you can also change the behavior from the view

Key points:

The above two methods can be applied to the Odoo server side, where you can change the existing view The behavior or any other content you can change in Odoo to see the effect on the client side.

I hope this helps you..

I am new to openERP and accepted an interview. Please explain the idea of ​​different types of inheritance in openERP, I think there are 3 types in total. Please explain it in a very simple way from the perspective of the interview.
PS: I am familiar with the concept of simple inheritance .

Inheritance:

The inheritance mechanism is used to create the concept of reusability. Reusability means that in any object-oriented programming Reuse the code of the parent class.

Benefits:

> Reduce code redundancy.
>Provide code reusability.
>Reduce source code size and improve code readability.
>Code is easy to manage, divided into parent and sub-categories.
> Support code scalability by overriding the base class
Functions in the subclass.

Disadvantages:

>In inheritance, the base class and the subclass are tightly coupled.
So if you change the code of the parent class, it will be affected
All children’s classes.
>In the class hierarchy, many data members remain unused and memory
assigned to them are not utilized .So it affects your performance.
Program, if you don’t implement inheritance correctly.

There are two ways of inheritance in OpenERP.

1. Classic Pythonic way:

p>

It allows adding specific “generic” behaviors to the Model by inheriting classes derived from orm.Model (such as geoModel with goegraphic support).

class Myclass(GeoModel, AUtilsClass):

Use _inherit: –

The main goal is to add new behaviors/extend existing models. For example, you want to add new fields to the invoice and add 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

Overwrite 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_stuf f

2.Polymorphic way: –

Use _inherits: –

When using _inherits, you will execute a polymorphic model in a database way.

For example, product.product inherits product.template or res.users inherits res.partner. This means that we have created a model that can obtain the knowledge of the model, but adds in the new database table Additional data/columns. So when you create a user, all partner data is stored in the res_partner table (and partners are created), and all user-related information is stored in the res_users table.

For this, we use dict: _inherits = {‘res.partner’:’partner_id’} This key corresponds to the basic model and the foreign key value of the basic model.

You can also inherit Odoo views through XML (Such as form view, tree view, search view, etc.), you can also change the behavior from the view

Key points:

The above two methods can be applied to the Odoo server side, you You can change the behavior of the existing view or any other content you can change in Odoo to view the effect on the client side.

I hope this helps you..

Leave a Comment

Your email address will not be published.