Set and get store data Odoo with TransientModel

Mariano DAngelo picture Mariano DAngelo · Feb 12, 2016 · Viewed 7.4k times · Source

I'm trying to store config data in odoo, I need to store 3 reference to 'account.journal'. The model is created in database, the view is shown in configuration base menu, the data is store in database when I push the APPLY button BUT when I reload the menu the data is not shown

The code use:

from openerp import fields, models, osv, api, _

class Configuration(models.TransientModel):
    _inherit = 'res.config.settings'
    _name = 'transporte_carta_de_porte.config.settings'

    ft_mercaderia = fields.Many2one(
        'account.journal',string='Debito ft mercaderia',
        help="Diario de ajuste al transportista por faltante de mercaderia")
    ade_transportista = fields.Many2one(
        'account.journal',string='Debito por adelanto transportista',
        help="Diario de debito al transportista por faltante de adelanto")
    ade_proveedor = fields.Many2one(
        'account.journal',string='Debito por adelanto proveedor',
        help="Diario de debito por adelanto en la cuenta del proveedor de combustible",)

The layout

<record id="view_tcp_config_settings" model="ir.ui.view">
    <field name="name">TCP settings</field>
    <field name="model">transporte_carta_de_porte.config.settings</field>
    <field name="arch" type="xml">
        <form string="TCP settings" class="oe_form_configuration">
            <sheet>
                <div>
                    <button string="Apply" type="object" name="execute" class="oe_highlight" />
                    or
                    <button string="Cancel" type="object" name="cancel" class="oe_link" />
                </div>
                <group string="Journals Settings">
                    <field name="ft_mercaderia" />
                    <field name="ade_transportista" />
                    <field name="ade_proveedor" />
                </group>
            </sheet>
        </form>
    </field>
</record>

<record id="action_tcp_configuration" model="ir.actions.act_window">
    <field name="name">TCP Configuration</field>
    <field name="res_model">transporte_carta_de_porte.config.settings</field>
    <field name="priority" eval="50" />
    <field name="view_mode">form</field>
    <field name="target">inline</field>
</record>

<menuitem id="menu_tcp_config" name="TCP Settings" parent="base.menu_config" action="action_tcp_configuration" />

Every time I choose a value for the fields and push Apply a new record is created insted of modify the first created and no one is load on the view load. Thanks for reading!

Answer

Ludwik Trammer picture Ludwik Trammer · Feb 12, 2016

TransientModels are designed to be temporary, just so you can get the values and do with them whatever you want to. They are periodically removed from the database.

You need to implement your own means of saving those settings. You need to implement (at least) two methods:

  • set_foo (where foo is an arbitrary string) for saving the values.
  • get_default_foo (where foo once more is an arbitrary string) for getting the saved values (for displaying them in the configuration user interface)

A simple example:

class AgeLimitSetting(models.TransientModel):
    _inherit = 'res.config.settings'

    min_age = fields.Integer(
        string=u"Age limit",
    )

    @api.model
    def get_default_age_values(self, fields):
        conf = self.env['ir.config_parameter']
        return {
            'min_age': int(conf.get_param('age_verification.min_age')),
        }

    @api.one
    def set_age_values(self):
        conf = self.env['ir.config_parameter']
        conf.set_param('age_verification.min_age', str(self.min_age))

ir.config_parameter (providing the set_param and get_param methods) is just a simple key-value store built into Odoo that let you store arbitrary strings. I used it as an example, but in reality you can store settings wherever you like.