-1

I have been spending my time in learning the development of OpenERP modules using Python language. I have come through a source code which I am not getting . I have tried a few sources for it but I didnot find any relevant help. Plz guide me to understand this code. The code is

def copy(self, cr, uid, id, default=None, context=None):
        if default is None:
            default = {}
        if context is None:
            context = {}
        default.update({'depreciation_line_ids': [], 'state': 'draft'})
        return super(account_asset_asset, self).copy(cr, uid, id, default, context=context)

Plz reply me as soon as possible. Best wishes

Arsalan Sherwani
  • 889
  • 2
  • 25
  • 60

1 Answers1

1

The referred answer is correct as far as the classic use of super in python is correct but OpenERP adds a couple of its own twists.

Remember that all your models inherit from osv.Model (or osv.TransientModel) so technically when you call super(account_asset_asset...) in the above code, it is going to call the copy method on osv.Model which is itself only a thin inheritance of osv.BaseModel so it is that copy method that will be called.

The code in osv.BaseModel.copy goes through the various installed modules that inherit account_asset_asset and dynamically execute the copy methods on those in sequence. This is usually the last installed module first, working its way up to osv.BaseModel but you can use sequences to control this I think.

So.. when you are overriding ORM methods such as copy, create etc., the result of a super call is to call the same method in the previously installed module that inherits the same model. Put simply, ignore the classic Python inheritance of osv.Model and look at the _inherit variable on the model.

A couple of gotcha's here - sometimes, an inherited model will add in extra named arguments into the method signature so make sure you specifiy named and un-named arguments correctly.

If you are over-riding copy, create etc., make sure you also return the result of the super call or you will kill the inheritance chain.

user229044
  • 232,980
  • 40
  • 330
  • 338
Adrian Merrall
  • 2,509
  • 12
  • 9
  • but I still dont get the idea of using super keyword in this piece of code – Arsalan Sherwani Dec 02 '13 at 10:45
  • hey , do you know the function of _check_recursion() in OpenERP development under Python – Arsalan Sherwani Dec 02 '13 at 12:42
  • Regarding the purpose of it, the create method is called automatically when you create a new object (i.e. insert it into the database). This piece of code changes a couple of fields, depreciation line ids and state and then calls super to get the ORM later to actually do the create. _check_recursion is another whole question. – Adrian Merrall Dec 02 '13 at 22:03
  • can u define the use of _check_recursion as there is no useful source help available – Arsalan Sherwani Dec 04 '13 at 07:12
  • Please stop signing your posts. [Signatures are not allowed here, your posts are already signed with your user card](http://stackoverflow.com/help/behavior). – user229044 Dec 04 '13 at 16:41