Duration field should be filled automatically when start and end time is given a fieldtype in dtype?


Hi @adi09 ,

Please write this code on validate.

I added this code to my doctype “service2”:

  1. I am adding above code to service2.py
  2. then did bench --site s.com migrate
  3. created new doctype

Problem: Duration is not calculated automatically

Here service2.py which is .py file of my doctype “service2” in my custom app “seva_management”.

Please tell me what i am missing or doing wrong?

Hi @adi09 ,

In which doctype you have added a fields? In that doctype has .py file. In that .py file you have to write this function and go to the hooks.py file of seva_management app and run this code on validate event in docevents.

Hey in my service2.py file i have added your code:

And in my hooks.py file I have added following code:

After doing bench migration and while creating a new form. I am getting this error:

Kindly help me with this, i am a beginner.

Waiting for your response!

Hi @adi09 ,

Please change code to

def calculate_duration(self, method):

   Start_date = self.start_date
   End_date = self.end_date 

Please changes this and keep rest of all as it is.

Hope this will helpful

Thank You

@Dhruvin-Bhaliya Thanks for the response. Actually that also i have tried but it gives me different error:

Also when I write

def calculate_duration(self, method)

In this method is not accessed.

Hi @adi09 ,

Have you changed a datatype of duration field ? If not then i think your old entries are affected. How many records you have created in this doctype? If not more then delete duration field from custom field and also from the database and try to add again that field and then check.


Thanks @Dhruvin-Bhaliya the issue is resolved!
I used controller(on_update) instead of hooks in my service2.py and it worked!