# Configuration Model without Multivalue or ModelSingleton

**URL:** <https://discuss.tryton.org/t/configuration-model-without-multivalue-or-modelsingleton/475>\
**Category:** Feature\
**Created:** [January 8, 2018, 3:37pm UTC](https://discuss.tryton.org/t/configuration-model-without-multivalue-or-modelsingleton/475 "2018-01-08T15:37:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![oscar](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/oscar/32/1919_2.png) [@oscar](https://discuss.tryton.org/u/oscar)\
**Post date:** [January 8, 2018, 3:37pm UTC](https://discuss.tryton.org/t/configuration-model-without-multivalue-or-modelsingleton/475/1 "2018-01-08T15:37:14Z")

</div>

## 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

> **[ModelSingleton vs MultiValue (#6984) · Issues · Tryton / Tryton · GitLab](https://foss.heptapod.net/tryton/tryton/-/issues/6984)**
>
> There is a bad performance combination between ModelSingleton and MultiValue. The singleton is frequently instantiated using '1' as id with the idea that it does not cost...

## 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](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

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [January 8, 2018, 4:42pm UTC](https://discuss.tryton.org/t/configuration-model-without-multivalue-or-modelsingleton/475/2 "2018-01-08T16:42:08Z")

</div>

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: [Remove company record rules (#4080) · Issues · Tryton / Tryton · GitLab](https://bugs.tryton.org/issue4080)
- Do not use the cache. A search is done on each access to the configuration.

---

<div class="post-metadata">

**Author:** ![oscar](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/oscar/32/1919_2.png) [@oscar](https://discuss.tryton.org/u/oscar)\
**Post date:** [February 11, 2018, 2:59pm UTC](https://discuss.tryton.org/t/configuration-model-without-multivalue-or-modelsingleton/475/3 "2018-02-11T14:59:29Z")

</div>

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: [ModelSingleton vs MultiValue (#6984) · Issues · Tryton / Tryton · GitLab](https://bugs.tryton.org/issue6984)

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [February 11, 2018, 4:00pm UTC](https://discuss.tryton.org/t/configuration-model-without-multivalue-or-modelsingleton/475/4 "2018-02-11T16:00:29Z")

</div>

I can not follow where you want to go.

> [@oscar](#):
>
> The start idea is not remove Multivalue feature of core Tryton, just in several configuration models

But all most all of them need MultiValue.

> [@oscar](#):
>
> We can add some class style “ModelSingleton” or any variable in setup or register that automatic create a record configuration when is missing one

What is the point? ModelSingleton manage that already.

> [@oscar](#):
>
> or the company is created.

But company is not the only contextual switch.

> [@oscar](#):
>
> We can add company “Unique” sql constraint to configuration model, with this avoid duplicate company configuration.

But we can want to have other switches than company.

> [@oscar](#):
>
> Ok but we can add a new cache attribute to this models

For what purpose?
