Configuration Model without Multivalue or ModelSingleton

Rational

I think that the current way for set sequences on configuration models need Multivalue, CompanyMultiValueMixin, CompanyValueMixin, default_func but this can be redesign to an easy way, removing indeed ModelSingleton

https://bugs.tryton.org/issue6984

Proposal

I propose not use ModelSingleton and Multivalue fields in configuration Models, instead set company by each configuration record, in other words it is simply replaced by ModelView and ModelSQL, using for example following structure:

class MyModelConfiguration(ModelSQL, ModelView):
    company = fields.Many2One(.....) # required=True
    sequence_foo = fields.Many2One('ir.sequence',....)
    parameter1 = fields.Char(....)

@staticmethod
def default_company():
    return Transaction().context.get('company')

@classmethod
def get_configuration(cls):
    company_id = Transaction().context.get('company')
    if company_id:
        config, = cls.search([
            ('company', '=', company_id)
        ])
        return config

So the end we will see:

My Configuration (View tree)

companyA | sequenceA1 | field_fooA | sequenceA2 | field_barA
companyB | sequenceB1 | field_fooB | sequenceB2 | field_barB
companyC | sequenceC1 | field_fooC | sequenceC2 | field_barC
. . .

Here real example:
https://bitbucket.org/presik/trytonpsk_hotel/src/b092145d3396b7e64da07bd68baed22f5aa399c5/configuration.py?at=default&fileviewer=file-view-default

For me is a simple solution, easy understand, easy coding, easy debugging, easy to migrate, works with multiple companies, and the moment does not require API changes

Implementation

I see many drawback for such design:

  • Prevent to extend to add other criteria than company which is solved by MultiValue
  • Do not work if no record is created. ModelSingleton allows to have default value even if no record was added.
  • Does not guarantee the uniqueness of the configuration.
  • Depend on the context only. MultiValue is more elaborated to be able to work without context: Issue 4080: Remove company record rules - Tryton issue tracker
  • Do not use the cache. A search is done on each access to the configuration.

Ok, I think those drawbacks can be solved, I am not expert, but I try search a solution for each one:

  • The start idea is not remove Multivalue feature of core Tryton, just in several configuration models
  • We can add some class style “ModelSingleton” or any variable in setup or register that automatic create a record configuration when is missing one, or the company is created.
  • We can add company “Unique” sql constraint to configuration model, with this avoid duplicate company configuration.
  • I dont know in which case context is not setted, but the piece code can be modified for above example:

@classmethod
def get_configuration(cls, company=None):
if not company:
company = Transaction().context.get(‘company’)
config, = cls.search([
(‘company’, ‘=’, company)
])
return config

  • Ok but we can add a new cache attribute to this models, for example:

class MyModelConfiguration(ModelSQL, ModelView):
__cache = True
company = fields.Many2One(…) # required=True
sequence_foo = fields.Many2One(‘ir.sequence’,…)

I can work around this features, maybe If you agree I can to open a new issue, maybe this help with: Issue 6984: ModelSingleton vs MultiValue - Tryton issue tracker

I can not follow where you want to go.

But all most all of them need MultiValue.

What is the point? ModelSingleton manage that already.

But company is not the only contextual switch.

But we can want to have other switches than company.

For what purpose?