Set and get store data Odoo with TransientModel

走远了吗. 提交于 2019-12-03 08:57:39

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!