Ambiguity on the method on_change

Hi everyone , i want some help ;
i have 3 fields on a form
i want one field from them should be filled out due to the result of 2 another fields :
this is the field1 , which i want to fill it out due to the result of other fields:
field1 = fields.Selection('on_change_design','description', readonly = False,)

this is the method :

        @fields.depends('field1', 'field2','field3')
        def on_change_design(self):
            if((self.field2 =="W" or self.field2 =="B" or self.field2=="W/B") and self.field3.code != "14" ):
                tab1=[]
                tab1.append(("-","-"))
                tab1.append(("Pos1", "Pos 1A"))
                tab1.append(("Pos2", "Pos 2B"))
                return tab1
            else:
               if((self.field2 =="W" or self.field2 =="B" or self.field2=="W/B") and self.field3.code == "14" ):
                    tab1=[]
                    tab1.append(("-","-"))
                    tab1.append(("lw", "slPivot "))
                    tab1.append(("rw", "rPivot "))
                    tab1.append(("fw", "mltrans "))
                    return tab1
                else:
                    tab =[]
                    tab.append((" "," "))
                    return tab1

i was coded the field3 , mentionned on the method like this
field3 = fields.Many2One('module_name.classA', 'process')
the class ‘module_name.classA’ was coded like this :


class design_position(ModelSQL, ModelView):
    __name__ = 'module_name.classA'
    
    code = fields.Char('Code', translate=True,
        required=True)
    name = fields.Char('Name', translate=True,
        required=True)
    def get_rec_name(self, code):
        return self.code

My problem is the result was shown on the field1 don’t take on consideration the result of
"self.field3.code " which it can be 14 or =!14 , the result shown on the field1 took just the first condition if((self.field2 =="W" or self.field2 =="B" or self.field2=="W/B") and always the result shown was the instructions of the first condition .
any help will be appreciated , thanks .

In this case I will recomend using on_change_with_<field_name> as it can be used to compute the value of a field depending on the values of others (which should be added on depends).

But from your code I understand that you are tryton to change the values of a selection field, so probably you are interested with the selection change with feature.

For this you just need to set a method (not an on_change) and decorate it with fields.depends.

Something like:

field1 = fields.Selection('compute_selection','Description')

@fields.depends('field2', 'field3')
def compute_selection(self):
    values = [(None, '')]
    # Add the available values using your custom logic
    return values

is the name of the method is reliant that the result not shown like i want !
i tried with the method compute_selection : but as i said at the first the tryton form can’t see the result of the condition if self.field3.code !="14" or `` self.field3.code ="14"
the error was :

File "/trytond/modules/module_name/file.py", line 11293, in on_change_with_field1
   if(self.field2== "W" and self.field3.code !="14"):
AttributeError: 'NoneType' object has no attribute 'code'

The problem is that your method is called when field3 is empty so it has a None value.
THen your code crashed because it is not possible to access the code attribute of an empty relation.

You should update your code to support empty values of all fields, because it is possible that the users sets an empty value for them

my problem that the field1 doesn’t make on consideration the depends of the field3 if it "14" or if it 's "!=14" , i want to know is there a something oblige me to define this field3 in the begining of the method (this field 3 is calling a class) before start coding the block " if " i was coded the field3 , mentionned on the method above like this:
field3 = fields.Many2One('module_name.classA', 'process')
the class ‘module_name.classA’ was coded like this :

class design_position(ModelSQL, ModelView):
    __name__ = 'module_name.classA'
    
    code = fields.Char('Code', translate=True,
        required=True)
    name = fields.Char('Name', translate=True,
        required=True)
    def get_rec_name(self, code):
        return self.code