Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

2

_inherits delegation OpenERP

By
omprakash
on 6/13/13, 5:12 AM 3,733 views
Hai Friends ,

     First I like to thank everyone for such good support for newbies . I have question regards _inherits delegation in OpenERP . 

1. For which purpose we make use of _inherits = {  'tiny.object_a': 'object_a_id' }  & _inherit = ['objectname']  ? Please explain with suitable example ?

2. After viewing code , had Question in my mind ..

         **sample code : addons / hr /hr.py**

class hr_employee(osv.osv):
    _name = "hr.employee"
    _inherits = {'resource.resource': "**resource_id**"}
    _columns = {
                     '**resource_id**': fields.many2one('resource.resource', 'Resource', ondelete='cascade', required=True),
                .................

                         }

    ......

     def unlink(self, cr, uid, ids, context=None):
              resource_ids = []
              for employee in self.browse(cr, uid, ids, context=context):
              resource_ids.append(employee.resource_id.id)
               return self.pool.get('resource.resource').unlink(cr, uid, resource_ids, context=context)
  ..........
  hr_employee()

 **In addons / hr /hr_view.xml** 

         Fields - **resource_id**  ( have not been used )

Actually what is described in this code . Please explain . If you explain both _inherit & _inherits  it will be more useful for understanding .. Please help me.


Thanks & Regards
 OMPRAKASH.A
3

Sudhir Arya (SA)

--Sudhir Arya (SA)--
10150
| 6 8 8
Ahmedabad, India
--Sudhir Arya (SA)--

Working as an OpenERP/Odoo developer and a Team Leader 

Top 5 Odoo contributor On Stackoverflow

LinkedIn

Blog

Stackoverflow

Sudhir Arya (SA)
On 6/13/13, 9:29 AM

If you want to extend or customize anything for particular object/class _inherit is used. It is a single object/class inheritance.

When you use _inherit and add new fields, those fields will be added inside inherited object. No other table will be created in database.

For example I want to add one or more field(s) or I want to add/override method in sale.order then I will use:

class sale_order(osv.Model):
    _inherit = 'sale.order'
    _columns = {
        'my_field': fields.char('My New Field', size=50),
    }

    def my_new_method(self, cr, uid, ids, context=None):
        ...
        ...

If I want to use anything (fields or methods) of any object and I want to achieve multiple inheritance, then I will use _inherits which allows you to use features of any object. That means you can directly use fields and methods of inherited object.

When you use _inherits and new table is created in database. That will have your own fields and field ID of inherited object.

For example in hr.employee object resource.resource object is inherited. So you can see fields of hr.employee object and resource_id of resource.resource object.


Now lets talk about code you want to know.

def unlink(self, cr, uid, ids, context=None):
          resource_ids = []
          for employee in self.browse(cr, uid, ids, context=context):
          resource_ids.append(employee.resource_id.id)
           return self.pool.get('resource.resource').unlink(cr, uid, resource_ids, context=context)

This method will be called when I will delete any employee record.

When any employee record is deleted, resource record related to employee will also be deleted.

Have a look at What is _inherit and What is _inherits.

Hope my answer will help you.

Hi Sudhir Arya , Thanks for immediate reply . You are such good friend for me . I will try in my code , If any doubt raise in my mind i will ask you . And once again thanks for reply

omprakash
on 6/14/13, 2:40 AM

Hi Sudhir Arya , Finally i like to conclude - As you mentioned _inherits has the property to access (methods) of any object . Can you please explain with example . Please still not clear at this point .

omprakash
on 6/14/13, 4:15 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 6/13/13, 5:12 AM
Seen: 3733 times
Last updated: 7/10/15, 3:25 AM