Set and get store data Odoo with TransientModel

后端 未结 1 1615
谎友^
谎友^ 2021-02-11 04:10

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, th

1条回答
  •  孤独总比滥情好
    2021-02-11 04:44

    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.

    0 讨论(0)
提交回复
热议问题