Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
1080 Lượt xem

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!

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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.

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 9 17
12628
2
thg 4 15
30341
1
thg 5 23
3172
2
thg 3 18
5110
0
thg 3 15
3641