Create an empty selection to be inherited

Viewed 171

I have created a parent model, which has a Selection field. This pattern must be inherited from classes that offer a service, so in the child classes I use fields.Selection(selection_add = ....). Is it possible to create, in the parent class, an "empty" selection that will only fill with the selection fields of the models that inherit it? Ex

class GenericService(models.Model):
_name = 'generic.service'
selection_type = fields.Selection(
[('select_type', _("Select an Account")),],default='select_type',                                                                                                                                                                                                                                              
    string=_("Service Type"),                                         
    required=True   

class SpecificService(model.Model):
_inherit = 'generic.service'

  selection_type = fields.Selection(
        selection_add=[('sftp','SFTP')],
    )

I would like to avoid adding a default field on the generic model selection, my alternative is to create a method that controls the insertion, but I would like to know if it could be avoided.

2 Answers

If you inherit like that _inherit without _name. Then you end up only with 1 model generic.service And all selection_add=[] added selections.

If you add

class GenericService(models.Model):
_name = 'generic.service'
 selection_types = fields.Selection([], string="Service Type")
  

class SpecificServiceA(model.Model):
_inherit = 'generic.service'

  selection_type = fields.Selection(
        selection_add=[('sftp','SFTP')], required=True
    )
class SpecificServiceB(model.Model):
_inherit = 'generic.service'

  selection_type = fields.Selection(
        selection_add=[('https','HTTPS')], required=True
    )

you end up with model named "generic.service" and selections [('sftp','SFTP'), ('https','HTTPS')]

I believe strings and selection names are added automatically to translation list. no need for _()

You can also use Odoo's mixin feature, which is a bit like abstract types/classes. Following an example in context of your requirments:

class ServiceMixin(models.AbstractModel):
    _name = "service.mixin"
    
    def _get_type_selection(self):
        """ Returns a 2-tuple list for type_selection field
        
            Meant to be extended by inheriting models."""
        return []
    
    type_selection = fields.Selection(
        selection="_get_type_selection", string="Type")

class ServiceA(models.Model):
    _name = "service.a"
    _inherit = ["service.mixin"]
    
    def _get_type_selection(self):
        """ Returns a 2-tuple list for type_selection field"""
        return [('a', 'A'), ('b', 'B')]

class ServiceB(models.Model):
    _name = "service.b"
    _inherit = ["service.mixin"]
    
    def _get_type_selection(self):
        """ Returns a 2-tuple list for type_selection field"""
        return [('a', 'A'), ('c', 'C')]

In database that will lead to 2 new tables: service_a and service_b. So you have really 2 new Odoo models, which aren't dependend on eachother.

Related