Skip to content

Latest commit

 

History

History
60 lines (43 loc) · 2.26 KB

11_onChangeComputed.md

File metadata and controls

60 lines (43 loc) · 2.26 KB

On change function

  • If we choose a patient name while creating a new appointment, it will automatically fill the Note field and if we remove the patient it will be empty. This task is done using onChange function.

    @api.onchange('patient_id')
      def _change_appointment_note(self):
          if self.patient_id:
              if not self.note:
                  self.note = "New appointment"
          else:
              self.note = ""

    onChange

  • If we set Note as a readonly field and save it will not store the data of Note. To save we have to use force_save="1" in the form view of the Note field.

    <field name="note" readonly="1" force_save="1"/>

Computed fields

  • To compute the appointment of an individual doctor we have to find in the appointment table based on doctor id. Here appointed_doctor_id will come from the appointment model which is a Many2one field.

    total_appointments = fields.Integer(string='No. of appointments', compute='_compute_appointments')
    
    # compute appointments of individual doctor
    def _compute_appointments(self):
        for record in self:
            record.total_appointments = self.env['kmhospital.appointment'].search_count(
                [('appointed_doctor_id', '=', record.id)])
  • We have use a for loop in the computed field as self contains multiple values otherwise it will arise Singleton Error.

  • In the view file we have to add the field of total_appointments inside the Patient List tab.

    <group string="No. of Appointments:">
        <h2>
            <field name="total_appointments"/>
        </h2>
    </group>

    computed1

Task

🚀 Happy Coding ! 🔥