Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
194 Widoki

I was trying to edit a selection field on my custom module via python code.


Original Field Definition

unit_of_measurement = fields.Selection(

​string='Unit of Measurement',

   selection=[

       ('meter', 'Meter'),

       ('feet', 'Feet'),

   ],

 )


I changed it to:

unit_of_measurement = fields.Selection(

​string='Unit of Measurement of to get Size Area',

   selection=[

       ('square_meter', 'Square meters'),

       ('squre_feet', 'Square feet'),

   ],

 )


What Happened After the Update:

When you updated your custom module, Odoo did not remove the old options. Instead, it combined both the old and new selection values,

Options became:
- meter
- feet
- square_meter
- squre_feet


Can someone explain to me what could be the cause and how can I resolve it? Thank you in advance!

Awatar
Odrzuć
Najlepsza odpowiedź

Identify the Field Type

  • A Selection field in Odoo is used to allow users to choose one option from a predefined list.
  • The options are usually static (hardcoded) or dynamically computed.

What Usually Goes Wrong

Common issues with Selection fields include:

  • The new value you're trying to assign is not in the list of allowed selections.
  • If the field value in the database doesn't match any current selection, it will throw an error.
  • When the selection values change in the code but existing records still have old or invalid values, it causes inconsistencies.

Root Cause (Most Likely)

  • The value being set or edited is not present in the current list of selection options.
  • Odoo checks if the value is valid according to the defined selections.
  • If the selection list was changed recently, or if data was imported/created with outdated values, it can lead to this issue.

How to Fix or Avoid It

  • Make sure that all values used in your records are present in the current selection list.
  • If you modify the selection list, check your database for any existing values that are now invalid.
  • You might need to write a script or use the Odoo shell to clean up or update invalid records.

Good Practice

  • When changing selection options, migrate existing data accordingly.
  • Always validate values before assigning them, especially if they come from user input or external sources.

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
2
wrz 17
11954
2
kwi 15
28359
1
maj 23
2314
2
mar 18
4508
0
mar 15
2887