Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9222731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:54:12+00:00 2026-06-18T03:54:12+00:00

This question is perhaps more complicated than it would initially seem. Let’s say I

  • 0

This question is perhaps more complicated than it would initially seem.

Let’s say I have a parent class Animal (we name it in OpenERP as animal.base). I also have subclasses Lion (animal.lion) and Elephant (animal.elephant). I need to be able to create a view with a many2one field that can refer to any subclass of Animal. This seems to work by doing the following:

class animal_lion(osv.osv):
    _name = 'animal.lion'
    _inherits = {'animal.base': 'base_id'}

    _columns = {
         ...
         'base_id': fields.many2one('animal.base', "Base ID")
    }

    def roar(self, cr, uid, context=None):
        print "rarrrrr"

Now when we create an instance of animal.lion we can see that it is visible in views that reference animal.base. (Plain inherit = 'animal.base' does not behave this way, FWIW.)

However, now let’s say we need to use a method from this animal. Since the many2one refers simply to animal.base, we have no idea what kind of animal the user selected in the view. Even if we happen to know that only lions will ever be chosen, we can’t call roar because the animal.base object will only let us call methods that are defined on itself. We can try to hack around it by naming the method emit_sound and trying to override the method in the Lion class. This will at least run (once you add an _inherit in addition to the _inherits) but it won’t produce the correct Lion-specific output. What is needed is some way to determine the dynamic type of the specific instance that was chosen in a many2one on base class x where multiple subclasses specify _inherits on that same class x. Imagine a fictional method get_subtype(). Then we can say the following in our button handler for the view:

def perform(self, cr, uid, ids, context=None):
    this = self.browse(cr, uid, ids[0], context)
    subtype_name = this.my_many2one.get_subtype()
    subtype = self.pool.get(subtype_name)
    # will produce a roar if user picked a lion, else a meep
    subtype.emit_sound(cr, uid, context)

Alternatively, is there any other architecture that can be used to accomplish the same task? (Yes, I contrived the example, but it should illustrate the real issue.) [Perhaps encode the subtype name in a field in each subtype instance? ]

I am restricted to OpenERP v5, but would be interested to know the answer for any version.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:54:13+00:00Added an answer on June 18, 2026 at 3:54 am

    The key here is that you want your base.animal to exist on its own in the database as a common index of all animals, so this complicates your data model substantially and forces you to use record-level inheritance (via _inherits).

    In order to resolve the subtype for an animal you should add an explicit type column in animal.base, and always set it properly so you can infer the subtype record.

    # This static list could also be replaced by a function
    ANIMALS = [
        ('lion', 'Lion'),
        ('elephant', 'Elephant'),
    ]
    class animal_base(osv.osv):
        _name = 'animal.base'
        _columns = {
             ...
             'type': fields.selection(ANIMALS, 'Type'), 
        }
    

    Your base.animals will exist in the database on their own and can have their own views, because you’re using a record-level inheritance. The subtype (e.g lion) can be seen as a “decoration” for each animal, and it might actually not be unique (both a base.lion and a base.elephant record could exist for the same base.animal record), so you should add uniqueness constraints somewhere.

    Now, you should never have both _inherit and _inherits pointing to the same parent model, these two inheritance schemes are really meant for different purposes, as explained in the OpenERP technical memento.
    Instead you can have proxy methods in animal.base that will roughly look like your perform method, except they need to find out the ID of the child records in addition to their type, e.g.:

    def emit_sound(self, cr, uid, ids, context=None):
        for this in self.browse(cr, uid, ids, context):
           animal_registry = self.pool['animal.%s' % this.type]
           animal_ids = animal_registry.search(cr, uid, 
               [('base_id','=',this.id)], context)
           assert len(animal_ids) == 1, 'Chimera alert! ;-)'
           animal_registry.emit_sound(cr, uid, animal_ids, context)
    

    Of course you could refine this in several ways, for example with function fields added to base.animal to automate more of this plumbing work.


    On the other hand, if you don’t really need the base.animal to exist next to the other real animals, but simply need a way to select an arbitrary animal in a form view, you could try to use the traditional inheritance with _inherit + _name, with base.animal being an abstract base class for them (without actually holding any record).
    Selecting an arbitrary animal could them be done with a fields.reference, on which you can filter the list of destination models. The 5.0 subscription module contains an example with the doc_source field.
    Watch out, fields.reference is something hybrid that does not integrate seamlessly with e.g. browse, read or search. It’s stored as a string in the form 'model,id' and you’ll have to split the value manually whenever you need to dereference it – so be careful if you go that path. The only place where it’s integrated as a pseudo-many2one is on the client UI, everywhere else it’s just a plain, dumb string value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is perhaps related to this question , but I have slightly more information.
Background/context As schema.org is relatively new, perhaps this question will promote more discussion than
This question is related to extending class methods in Ruby, perhaps more specifically in
This is perhaps more of a discussion question, but I thought stackoverflow could be
I have an ObjectDataSource (but perhaps this question is the same for all kinds
Let me first apologize if this question could sound perhaps sort of amateurish for
NOTE: This question has been updated to provide more detail and insight than the
Perhaps this question has been asked elsewhere, but I'm unable to find it. With
This question has been asked before perhaps multiple times, but I can't get the
This question is just out of interest, and perhaps could be useful for my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.