You are currently viewing Field Selection Odoo – Complete Guidance

Field Selection Odoo – Complete Guidance

Field Selection Odoo is today our topic. In Odoo, the Selection field type allows you to create a dropdown menu for a user to select from a predefined list of options. It’s a powerful tool to restrict values, making sure the data entered fits a specific set of values. Here’s a guide on how to create and use selection fields in Odoo, both in models and views.

Also Read:

Field Selection Odoo – Complete Guidance

1. Defining a Selection Field in the Model

To define a Selection field in Odoo, you use the fields.Selection class in your model. It’s typically defined with a list of tuples, where each tuple consists of two elements:

  1. The actual value that will be stored in the database.
  2. The label or display value that will be shown to the user in the form view.

Here’s an example of how you can define a Selection field in an Odoo model:

Example: Defining a Selection Field

python

from odoo import models, fields

class MyModel(models.Model):
_name = ‘my.model’

# Defining a selection field
status = fields.Selection(
[
(‘draft’, ‘Draft’),
(‘confirmed’, ‘Confirmed’),
(‘done’, ‘Done’),
(‘cancelled’, ‘Cancelled’)
],
string=“Status”,
default=‘draft’,
help=“The current status of the record”
)

In this example:

  • status is the name of the field.
  • The Selection field is defined as a list of tuples. For each tuple:
    • The first element ('draft', 'confirmed', etc.) is the value that will be saved in the database.
    • The second element ('Draft', 'Confirmed', etc.) is what will be displayed in the form view as a label.
  • default='draft' sets the default value for the field when a record is created.
  • string is used to define the label for the field in the view.
  • help provides a tooltip when you hover over the field in the UI.

2. Using the Selection Field in a Form View

Once the selection field is defined in the model, you need to add it to a form view so that users can select from the available options.

Example: Adding the Selection Field to a View

xml
<odoo>
<record id="view_form_my_model" model="ir.ui.view">
<field name="name">my.model.form</field>
<field name="model">my.model</field>
<field name="arch" type="xml">
<form string="My Model Form">
<sheet>
<group>
<field name="status"/>
</group>
</sheet>
</form>
</field>
</record>
</odoo>

In this XML code:

  • The <field name="status"/> tag adds the status field to the form view.
  • When rendered, it will display a dropdown menu with the four options: “Draft,” “Confirmed,” “Done,” and “Cancelled.”

3. Dynamic Selection Lists (Optional)

If you want the available selection options to be dynamic (i.e., they depend on another field or logic), you can use a computed function to return the options. Here’s how you can do it:

Example: Dynamic Selection Field Based on a Condition

python
class MyModel(models.Model):
_name = 'my.model'
status = fields.Selection(
selection=lambda self: self._get_status_options(),
string=“Status”,
default=‘draft’
)

def _get_status_options(self):
# Example logic for dynamic selection
if self.env.user.has_group(‘base.group_system’):
return [(‘draft’, ‘Draft’), (‘confirmed’, ‘Confirmed’), (‘done’, ‘Done’)]
else:
return [(‘draft’, ‘Draft’), (‘cancelled’, ‘Cancelled’)]

Here:

  • _get_status_options() is a method that returns a list of selection options based on some condition (e.g., user group).
  • If the user belongs to the system group (base.group_system), they will see the first set of options; otherwise, they’ll only see the “Draft” and “Cancelled” options.

4. Using the Selection Field in Action Logic

You can also use the selection field in your business logic, for example, to perform actions based on the selected value.

Example: Performing Actions Based on Selection Field Value

python
class MyModel(models.Model):
_name = 'my.model'
status = fields.Selection([
(‘draft’, ‘Draft’),
(‘confirmed’, ‘Confirmed’),
(‘done’, ‘Done’),
(‘cancelled’, ‘Cancelled’)
], string=“Status”, default=‘draft’)

def action_confirm(self):
for record in self:
if record.status == ‘draft’:
record.status = ‘confirmed’
# Additional logic here
else:
raise UserError(“Record is already confirmed or cancelled.”)

def action_cancel(self):
for record in self:
if record.status == ‘confirmed’:
record.status = ‘cancelled’
# Additional logic here
else:
raise UserError(“Record cannot be cancelled unless confirmed.”)

Here:

  • action_confirm() changes the status of the record to “Confirmed” if the status is “Draft.”
  • action_cancel() checks the status before changing it to “Cancelled.”
  • If the record is not in the correct status, it raises an error using UserError.

5. Tips for Using Selection Fields

  • Use meaningful values: When defining selection fields, use meaningful and intuitive labels. This will help users understand the choices better.
  • Limit the number of options: Too many options in a selection field can be overwhelming and lead to mistakes. Keep the options concise and relevant.
  • Localization: Odoo allows translation of field labels, which can be handy if your application supports multiple languages. Ensure the labels are translated appropriately using the translations option.
  • Limit to user roles: You can use dynamic selection options (as shown earlier) to restrict field values based on user roles or other business conditions.

6. Conclusion

The Selection field in Odoo is an essential tool for restricting and managing the values a user can select. It ensures consistency across records and simplifies data entry by offering users a clear and predefined list of options. By using the Selection field in both models and views, you can create highly customizable forms that match your business processes, while also ensuring data integrity.

For more information about the Field Selection Odoo – Complete Guidance, visit this link.

If you want to Free Trail Zoho, click on this link.

Yasir Baig

My name is Mirza Yasir Baig. As an experienced content writer and web developer, I specialize in creating impactful digital experiences. With expertise in WordPress programming and the MERN stack, I have built and managed various web platforms, including the different a dedicated resource for both Pakistani and international students seeking quality courses and training programs. My work is driven by a passion for education and technology, ensuring that content is not only engaging but also optimized for search engines (SEO) to reach a wider audience.

Leave a Reply