Pular para o conteúdo
Menu
Esta pergunta foi sinalizada
1 Responder
193 Visualizações

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!

Avatar
Cancelar
Melhor resposta

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.

Avatar
Cancelar
Publicações relacionadas Respostas Visualizações Atividade
2
set. 17
11954
2
abr. 15
28353
1
mai. 23
2313
2
mar. 18
4507
0
mar. 15
2887