Search code examples
odooselection

Create an empty selection to be inherited


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.


Solution

  • 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 _()