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

I have this field in my model:

class Embed_Ingredients (models.Model): 
    _name = 'afribon_productapp.embed_ingredients'
     _description = 'a description'

    GLOBAL_IMPORT_OPTIONS = [('less', '<1 ton'), ('greater', '> 1 ton')] 

    procurement = fields.Selection ( [('imported', 'Imported'), ('local', 'Local')], default = 'imported', required = True )

    freight_greater_or_less = fields.Selection (
        GLOBAL_IMPORT_OPTIONS,
        string = "Weight",
        default = 'less'
    )



    @ api.onchange (' procurement ')
     def procurement_change (self):
     ' ''
        When user changes procument to `local`, add a new value to the global variable GLOBAL_IMPORT_OPTIONS
    '' '
     if self.procurement ==' local ':
  
        self.GLOBAL_IMPORT_OPTIONS.append ((' full_load_container ',' Full Load Container '))         
        print (self.GLOBAL_IMPORT_OPTIONS)
                
        
           
        


When the 'procurement' selection changes to local, I would like to add a new value into the  GLOBAL_IMPORT_OPTIONS variable

When I print the value of GLOBAL_IMPORT_OPTIONS in the terminal, I can see that the value has been appended into the list, but on the form view, I cannot see the new value in the selection dropdown. 

Thanks!


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

Hello,

There are two ways to define dynamic values for selection field. You can pass the method through selection attribute inside fields. 

Like below mentioned code:

selection_field= fields.Selection(selection=lambda self: self.dynamic_selection())

Inside this code there is a method dynamic_selection

You can pass list of tuples like the method mentioned below

def dynamic_selection(self):

      select = [('yes', 'Yes'), ('no', 'No')]

      return select

Conclusion: if you ever want to at some dynamic values inside the selection field then you can simply inherit that method and add values as per your requirements.

Hope your query is resolved by now. Feel free to ask your questions. 

Thanks

Anisha Bahukhandi

Technical Content Writer

Ảnh đại diện
Huỷ bỏ

Perfect solution to change the values of a selection field. Thanks!

Câu trả lời hay nhất

there are 3 patterns in odoo code to get such thing it could as there

https://dev.to/kerbrose/dynamic-selection-list-in-odoo-for-selection-fields-16h8

Ảnh đại diện
Huỷ bỏ

nice work

Câu trả lời hay nhất

if anybody looking for how this works in odoo15, it's as Anisha said except you use:
selection=lambda self: self.env['your.model']._function_to_populate_selection()


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

Hi, 

You can follow following link for this:

https://youtu.be/GPhgxxwprA4

Hope it helps,

Thanks

Ả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 23
13113
2
thg 9 23
9309
2
thg 11 23
5347
1
thg 5 17
3359
1
thg 12 20
4881