简体   繁体   中英

Odoo 10 Custom Module Configuration Wizard not storing the settings

I want to create a configuration widget and store the settings in the database. I found this odoo development documentation for this topic but it does not seem to work...

Here is how my python code looks like:

class Configuration(models.TransientModel):
  _name = 'my.config'

  _inherit = 'res.config.settings'

  my_category = fields.Many2one('product.category', 'My Special Product Category')

  def get_default_params(self, fields):
    res = {}
    res['my_category'] = self.env['ir.config_parameter'].get_param('my_category', '').strip()
    return res

  @api.multi
  def set_my_config(self):
    self.ensure_one()
    value = getattr(self, 'my_category', '')
    self.env['ir.config_parameter'].set_param('my_category', value.id)

The method get_default_params is called as expected and the value of res is also as expected (I wrote it to the logoutput and it was like : {'my_category': u'100'} ). However, the value is not visible in the Configuration Wizard. :(

I now tried a workaround, including another field 'company_id' and setting the value of my_category' onchange of company_id (which is triggered once the wizard is opened. This solution works, but I'm not sure if that's a proper solution.

Any suggestion what I'm doing wrong with the approach described in the documentation?

try this

@api.multi
def set_my_category_defaults(self):
    return self.env['ir.values'].sudo().set_default(
        'ir.config_parameter', 'my_category', self.my_category.id)

I found my mistake & when looking at the logoutput (which I posted in the question) it was almost obvious: the reason why my method: get_default_params did not work was because the result returned was a string ( {'my_category': u'100'} but it expects an integer to work properly {'my_category': 100} .

So when casting the result, my wizard works as expected :)

def get_default_params(self, fields):
    res = {}
    cat = self.env['ir.config_parameter'].get_param('my_category', '').strip()
    if cat:
        res['my_category'] = int(cat)
    return res

Another possibility would be to store the value in a different table (as the user ashvin suggested) -> then fetching the value will be in integer already, and no cast is necessary:

def get_default_params(self, fields):
    res = {}
    res['my_category'] = self.env['ir.values'].get_default('my.config', 'my_category')
    return res

@api.multi
def set_my_category_defaults(self):
    return self.env['ir.values'].sudo().set_default(
        'my.config', 'my_category', self.my_category.id)

Thank you for your help. Please comment, which of these solutions is programatically the better approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM