Skip to Content
Menu
This question has been flagged
3 Replies
5673 Views

Hi,
Can anyone please tell me how i can save value of many2many field in res.config.settings, I am getting issue "Invalid value for res.config.settings.authorized_users: 1" here is my code:
default_user_id = fields.Many2one('res.users', string='Authorized Users')
authorized_users = fields.Many2many("res.users",string='Authorized Users', domain=[('share', '=', False)])

@api.onchange('authorized_users')
    def _onchange_language_ids(self):
        if self.authorized_users and self.default_user_id not in self.authorized_users:
            self.default_user_id = self.authorized_users[0]


View file:
<div class="col-md-6">
       <field name="authorized_users" widget="many2many_tags" options="{'no_create': True, 'no_open': True}"/>
</div>

Avatar
Discard

which version of odoo you are using ?

Author

odoo 11 CE

Best Answer

Hello 

try with below code

import ast

class ResConfigSettings(models.TransientModel):
    _inherit = "res.config.settings"
   
@api.model
    def get_values(self):
        res = super(ResConfigSettings, self).get_values()
        user_ids = self.env['ir.config_parameter'].sudo().get_param('your_modulename.authorized_users')
        res.update(
            authorized_users=[(6, 0, ast.literal_eval(user_ids))],
        )
        return res
    
    @api.multi
    def set_values(self):
        super(ResConfigSettings, self).set_values()
        set_param = self.env['ir.config_parameter'].sudo().set_param
        set_param('your_module_name.authorized_users', self.authorized_users.ids)
Avatar
Discard
Author

I have tried above code

but there was issue related Malformed String, for this I have passed None if user_ids is false.

but still I am facing same issue "Invalid value for res.config.settings.authorized_users: 5"

here is my code:

@api.model

def get_values(self):

res = super(ResConfigSettings, self).get_values()

user_ids = self.env['ir.config_parameter'].sudo().get_param('my_modulename.authorized_users')

if user_ids == False:

res.update(

authorized_users=[(6, 0, ast.literal_eval('None'))],

)

else:

res.update(

authorized_users=[(6, 0, ast.literal_eval(user_ids))],

)

return res

you have write this code in if condition.

res.update(authorized_users=[(6, 0, [])],)

Author

"Malformed String" this issue is already get solved by using None as well.

but the problems remains same which i am getting while selecting user "Invalid value for res.config.settings.authorized_users: 5"

Author

Mitul have you done this kind of stuff before? ..

I really don't know why this is giving issue of invalid value, I tries with many ways. And really thanks for you help.

yes, i have done same thing for store the value of many2many field into res.config.settings. at that time i have use the code that i have post as an answer.

please remove your whole method (_onchange_language_ids(self) ) and then try with my code.

Author

Yes I have removed my methods, sorry i forgot to tell that invalid user issue is gone but it is passing always false value in user_ids of get_value() method & because of this nothing gets reflect.

go to the system parameter and check the value for the key "your_module_name.authorized_users" is exist or not.

Author

Yes its there

Thanks. Its Working.