Home > Enterprise >  How to sum time based on a given time in django?
How to sum time based on a given time in django?

Time:01-04

class Clock(models.Model):
report_submitted_at = models.TimeField()
delivery_time = models.TimeField(null=True, Blank=True)

I am new to Django so I am curious how to achieve this. In my model report_submitted_at time I am entering. Based on the entered time i want to save the delivery time automatically. Delivery time is always '04:00:00'. So suppose i entered report_submitted_time '02:00:00'. The delivery time will be '06:00:00' for that row.

I imported datetime. Manually i am able to add two times. But here how can i save the delivery time automatically. Any help would be really appreciated. Thank you !!

CodePudding user response:

You can use Django signals like this.

from django.dispatch import receiver
from django.db import models


@receiver(signal=models.signals.post_save, sender=Clock):
def set_delivery_time(sender, instance, created, **kwargs):
    if created:  # For new objects, it will be True. In case of updating old object, it will be false
        instance.delivery_time = instance.report_submitted_at.replace(hour=instance.report_submitted_at.hour   4)
        instance.save()
  •  Tags:  
  • Related