Show a selection field depending on another

How, to resolve an issue while executing the code in which i need to show an selection field depending on another field where patients age is coming in years.
The dependent field would show up only when age is between 18 to 35 yrs. To make the issue more clear to you, I am specifying you the code:

    date_of_birth = fields.Date('Date of Birth', help="YY/MM/DD")
    patient_female_age = fields.Function(fields.Integer('Patient Female Age'),
                                         'on_change_with_patient_female_age')
    doctor_talked_about_diabetes_pregnancy_is = fields.Selection(
        [
            (None, ''),
            ('yes', 'Yes'),
            ('no', 'No'),
            ('not_known', 'Not Known')
        ], """𝟱.Has your doctor talked to you
        about diabetes and pregnancy?""",
        sort=False, help="Select Perfect One",
        states={'readonly': (Eval('patient_female_age', 0) >= 18) & (
            Eval('patient_female_age', 0) <= 35),
        }, depends=['on_change_with_patient_female_age'])

    @fields.depends('date_of_birth')
    def on_change_with_patient_female_age(self, name=None):
        now = datetime.now()
        diff = relativedelta.relativedelta(now, self.date_of_birth)
        years = diff.years
        months = diff.months
        days = diff.days
       date_str = '{} years {} months {} days'.format(years, months, days)
        date_str = (int(str(date_str)[:2]))
        return date_str

    @staticmethod
    def default_patient_female_age():
        return 0

Any help regarding the issue will be appreciable.

date_of_birth = fields.Date(‘Date of Birth’, help=“YY/MM/DD”)

Females Who Are Between 18-35 Years Of Age

patient_female_age = fields.Function(fields.Integer('Patient Female Age'),
                                     'on_change_with_patient_female_age')
doctor_talked_about_diabetes_pregnancy_is = fields.Selection(
    [
        (None, ''),
        ('yes', 'Yes'),
        ('no', 'No'),
        ('not_known', 'Not Known')
    ], """𝟱.Has your doctor talked to you
    about diabetes and pregnancy?""",
    sort=False, help="Select Perfect One",
    states={'readonly': (Eval('patient_female_age', 0) >= 18) & (
        Eval('patient_female_age', 0) <= 35),
    }, depends=['on_change_with_patient_female_age'])

Age Calculater

@fields.depends('date_of_birth')
def on_change_with_patient_female_age(self, name=None):
    now = datetime.now()

    print(self.date_of_birth)
    diff = relativedelta.relativedelta(now, self.date_of_birth)
    print(diff, 'kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk')
    years = diff.years
    print(years, 'ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ')
    months = diff.months
    days = diff.days

    date_str = '{} years {} months {} days'.format(years, months, days)
    date_str = (int(str(date_str)[:2]))
    print(date_str, 'RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR')
    return date_str

@staticmethod
def default_patient_female_age():
    return 0

Assuming that ‘on_change_with_patient_female_age’ is returning the correct age, I think that you should modify the states and the depends of the ‘doctor_talked_about_diabetes_pregnancy_is’ field to something like:

states={'invisible': (Eval('patient_female_age', 0) < 18) | (
        Eval('patient_female_age', 0) > 35),
    }, depends=['patient_female_age'])

If it doesn’t solve the problem, can you specify if the problem is due to the functional field or if it’s returning an error, can you show us the traceback?

1 Like

Thanks for your help, it’s working @oscarQ !!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.