
Learn how to implement the Radio widget in Odoo to allow users to select a single option from multiple choices in your custom modules
The Radio widget in Odoo is a powerful user interface element that allows users to select a single option from a predefined set of choices. This widget is especially useful when you want to present multiple options in a compact manner and ensure that the user can only select one option at a time. In this blog post, we will explore how to implement the Radio widget in your custom Odoo modules.
To create a Radio widget in Odoo, you first need to define a selection field in your model. The selection field will hold the values you want to present to the user. Here’s how you can define a selection field in your Odoo model:
from odoo import models, fields
class MyModel(models.Model):
_name = 'my.model'
radio_option = fields.Selection(
selection=[
('option1', 'Option 1'),
('option2', 'Option 2'),
('option3', 'Option 3'),
],
string="Choose an Option",
default='option1'
)
In this example, we define a model called my.model with a field radio_option. This field is a selection field containing three options: Option 1, Option 2 and Option 3. We set default='option1' to have the first option selected by default.
Next, you need to update the view to utilize the Radio widget. To display the selection field as a radio button in the form view, you can specify the widget attribute in the XML view definition:
<record id="view_my_model_form" model="ir.ui.view">
<field name="name">my.model.form</field>
<field name="model">my.model</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<field name="radio_option" widget="radio"/>
</group>
</sheet>
</form>
</field>
</record>
This XML snippet adds the radio_option field to the form view and specifies that it should be displayed as a radio button using the widget="radio" attribute. When the form is rendered, the user will see the options as radio buttons, allowing for easy selection.
When the user selects an option from the radio buttons, the selected value is stored in the radio_option field in the database. You can easily access this value in your business logic or when performing operations based on user selections. For example:
@api.onchange('radio_option')
def _onchange_radio_option(self):
if self.radio_option == 'option1':
# Perform some action for Option 1
pass
elif self.radio_option == 'option2':
# Perform some action for Option 2
pass
In this example, we use the @api.onchange decorator to trigger an action whenever the radio_option field is changed. You can customize the logic within this method to fit your specific requirements.
The Radio widget in Odoo provides a user-friendly way to present multiple options to users, ensuring they can select only one at a time. By following the steps outlined in this blog, you can easily implement a radio button selection in your custom modules, enhancing the user interface and experience of your Odoo applications.
Your email address will not be published. Required fields are marked *