How to keep selected boolean fields based on computed result in odoo 10?

Multi tool use
How to keep selected boolean fields based on computed result in odoo 10?
I have a scenario, where need to keep selected boolean fields based on computed result before saving record, so how can i do this?
Here is my code :
option = fields.Boolean(string='option')
selected_option = fields.Boolean(compute='check_value', string='Selected')
@api.multi
def check_value(self):
for result in self:
if result.option == True:
result.selected_option = True
result.selected_option = False
check_value
selected_option
False
3 Answers
3
Please try this Code :
option = fields.Boolean(string='option')
selected_option = fields.Boolean(compute='check_value', string='Selected')
@api.depends('option')
def check_value(self):
for result in self:
if result.option:
result.selected_option = True
else:
result.selected_option = False
@Mani In python, it is better to use
if result.option
instead of if result.option == True:
as you can find at python.org/dev/peps/pep-0008 Don't compare boolean values to True or False using ==
– WaKo
Jul 2 at 12:17
if result.option
if result.option == True:
Don't compare boolean values to True or False using ==
selected_option = fields.Boolean(compute='check_value', string='Selected', store=True)
Write the onchange function for Boolean field (Option)
option = fields.Boolean(string='option')
selected_option = fields.Boolean(string='Selected')
@api.onchange('option')
def check_value(self):
for result in self:
if result.option == True:
result.selected_option = True
else:
result.selected_option = False
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Correct the indentation to understand what
check_value
do, because it seems that your code always sets theselected_option
toFalse
.– WaKo
Jul 2 at 8:24