2

I am using Odoo 12, and trying to display to selection-fields, in formView,

first selection field has 2 values, but second selection field has 4 values, 2 for each choosen field from first-selection-field.

I am giving dummy-code, as I can't show production-code.

  1. mainfile.py contents are

    class MainClassFile(models.Model):

            first_field = fields.Selection([ ('first':'Item-1'), ('second':'Item-2')],string="first-Selection")
    
            second_field = fields.Selection([('1','one')
                                                 ,('2','two')
                                                 ,('3','three')
                                                 ,('4','four')],
                                                string='Only-two-fields')
    
    
    @api.onchange('first_field')
    def show_two_fields_only(self):
        if self.first_field == 'first':
            return {'domain':{second_field = fields.Selection([('1','one')
                                                     ,('2','two')]
       else 
           return {'domain':{second_field = fields.Selection([('3','three')
                                                     ,('4','four')]
    
  2. xml file for same py-file having form-view as:

           <group name="default" col="4" colspan="2">
                                   <field name="first_field" required="1" default_focus="1"  onchange="show_two_fields_only('first_type'/>
                                   <field name="second_field" required="1"/>
           </group>

how can I domain filter second_field, based on value choosen from first_field,

eg. if first_value choosen is 'first' then form should show only second_field values as (1,2),

if first_value choosen is 'second' then form should show only second_field values as (3,4),

as dropdown selection list?

GD- Ganesh Deshmukh
  • 1,456
  • 3
  • 24
  • 36
  • I tried @api.onchange function_name, but can't find logic to select specific second_field, based on first_field choosen. – GD- Ganesh Deshmukh Oct 18 '19 at 07:58
  • 1
    Just create a model that holds the values of the selection. And convert your field to many2one – Charif DZ Oct 18 '19 at 10:31
  • yeah, I am working on that one only. :) can you show that please? – GD- Ganesh Deshmukh Oct 18 '19 at 10:36
  • Just imagine that the selection are records, selection field are used when the options are fixed and not large but if the options are large and can be updated here a selection field is not the answer we need a table to hold that options then create a many2one relation between two tables. – Charif DZ Oct 18 '19 at 11:06

0 Answers0