This question has been flagged
1 Reply
13252 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
Avatar
Discard
Best Answer

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.

Avatar
Discard
Author

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

Author

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 .