Zeck
Zeck

Reputation: 6579

How to implement abstract class on OpenERP?

I'm trying to implement an abstract class on OpenERP 6. But I don't have any idea. For example. I have "a" and "b" classes. "b" class inherits "a" (See below code)

class a(osv.osv):
    _name = 'a'
    _columns = {
            'state': fields.selection(A_WORKFLOWS, 'State', readonly=True)
        }

# create an object
a()

class b(osv.osv):
    _name = 'b'
    _inherit='a'
    _columns = {
            'name' : fields.char('Name', size=64, required=True)
        }

# create an object
b()

When I upgrade module "a" class generated in database. I don't want it. I want to use the "a" class like abstract class in OpenERP.

Upvotes: 1

Views: 2076

Answers (5)

ifixthat
ifixthat

Reputation: 6295

From OpenERP 6.1 we have AbstractModel class.Sp you can inherit your class from Orm and make is Abstract. For previous Version like noteed you can use osv_memory object which is very slick option you can have.

Or Either way you can create python class inherited form object and then you can inherit your object from osv + object class and their you go. e.g.

class AbstractClass(object):
    def a():pass
    def b():pass

class RealClass(osv.osv, AbstractClass):
    def a():pass
    def b():pass

While Inheriting please take care of mro and other inheritance rule.

Thanks.

Upvotes: 0

noteed
noteed

Reputation: 375

Starting with OpenERP 6.1 (the upcoming release), openerp.osv.orm is providing an AbstractModel class. Before that, it was possible to achieve what you want by using an osv_memory as a base class.

Upvotes: 1

Daniel Reis
Daniel Reis

Reputation: 13342

I would go ahead with the solution you presented. It has the inconvenience of creating the a table on the database, but in my opinion that's irrelevant because it won't take up storage space, since the a model will not be populated with data.

You can also try an alternative: to declare the a class common columns only in the inherited models (b in your example). This is used in the official modules using two different techniques: check in the crm module, the crm_lead model, that inherits the crm_case in python style, and mail_thread in OpenERP style.

Upvotes: 2

Yajushi
Yajushi

Reputation: 1185

as per my knowledge, In openerp there is no specific concept for abstarct class except python "abstract base class" concept.

inside openerp code if you will check in "orm.py" file. there are some of the methods with "Not Impemented" or "raise NotImplementedError" exception, which can be considered as an abstract methods, same as of python code.

so basically, you need to use python concept for openerp.

you can implement abstract base class by two different methods:

One:

using "abc"

Two :

using "NotImplementedError" exception

class AbstractC(object):
    """abstract base class"""

    def abcMethod(self):
        raise NotImplementedError( "Not implemented in base class, overwite the same method in child class" )

Upvotes: 0

c0m4
c0m4

Reputation: 4443

If you want class b to inherit from a you should do

class b(a):

Upvotes: 0

Related Questions