Enable Dark Mode!
how-to-work-with-selection-fields-in-odoo-19.jpg
By: Safa KB

How to Work with Selection Fields in Odoo 19

Technical Odoo 19 Fields and widgets

In Odoo 19, a selection field is a kind of field where users can choose from a defined set of values, rather than typing arbitrary text. This will make the data uniform, filterable, and faster to operate on in general among different modules. This is useful when you want the user to select between two or more options, like the kind of vehicle: either a car or a bike.

How to Define a Selection Field

Selection fields are defined in your Python models using fields.Selection. Each option is stored internally as a technical value but displayed to the user as a readable label.

Example: Basic Selection Field

vehicle_type = fields.Selection(
[('car', 'Car'), ('bike', 'Bike')]
default='car',
required=True,
tracking=True
)
  • car/bike - stored in the database.
  • Car/Bike - to be displayed in UI.
  • Default value - car
  • Required - cannot be left empty.
  • Tracking - changes will appear in the chatter.

Handy Properties of Selection Fields

Odoo provides several attributes to make fields.Selection is both flexible and powerful:

  • selection: The options available can be a static list, a method that returns a dynamic list, or even a global constant.
  • string: The label displayed to users, such as "Power Unit".
  • default: Automatically assigns a default value when a new record is created.
  • required: Ensures that the field must be filled before saving.
  • tracking: Logs changes to the field in the chatter for better traceability.
  • compute: Computes the field value dynamically based on other fields.
  • selection_add: Adds new options to an existing field without overriding the original ones

Real Examples from Odoo 19

Practical examples in Odoo 19 - inspired by the Fleet Vehicle Model - are as follows:

Static list

A static selection list is a fixed set of predefined options that never change, such as choosing between Manual and Automatic transmission.

transmission = fields.Selection(
[('manual', 'Manual'), ('automatic', 'Automatic')],
string='Transmission'
)

Dynamic list

A dynamic selection list is generated automatically at runtime, for example, creating a list of model years from 1970 up to the current year.

def _get_year_selection(self):
current_year = datetime.now().year
return [(str(i), i) for i in range(1970, current_year + 1)]
model_year = fields.Selection(
selection='_get_year_selection',
string='Model Year'
)

Extend existing field

Using selection_add, you can extend an existing selection field by adding new options—like adding a new state 'c' without overriding the original selection values.

state = fields.Selection(
selection_add=('c', 'C')
default=None
)

Displaying Selection Fields in Views

Once you've defined it, add the field to your form view via XML:

<field name="state" widget="statusbar"/>

You can also modify the appearance by using widgets such as:

  • statusbar: for progress stages
  • radio: for quick selection
  • badge: for a tag-like display

Why use selection fields?

  • They standardize values, making them typo- and inconsistency-free.
  • They simplify forms for the end users by providing clear choices.
  • They improve reporting and filtering since data is stored in a controlled way.
  • They allow for easy customization because developers can extend options using the selection_add function.

Conclusion

The selection fields in Odoo 19 give a proper structure to capture the user's choices. Be it a simple dropdown, a dynamic list, or extended states for workflows and fields.Selection gives the flexibility to build clean and user-friendly models. Mastering these attributes, like selection, string, default, required, tracking, compute, and selection_add, will make any Odoo application more reliable and efficient.

To read more about How to Create an Advanced Selection Field in Odoo 18, refer to our blog How to Create an Advanced Selection Field in Odoo 18

FAQ

Q1. Can I generate selection values dynamically?

A: Yes, by using a method that returns a list of tuples.

Q2. How do I add new options to an existing selection?

A: Use the selection_add attribute while inheriting a model.

Q3. Can selection fields be tracked in the chatter?

A: Yes, just add tracking=True to the field definition.

Q4. What happens if no default value is set?

A: This field will remain empty until the user has chosen an option.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message