H
H
H2CO32021-04-08 18:25:50
Django
H2CO3, 2021-04-08 18:25:50

How to calculate the time span between two time records?

I expanded the Profile model and added two "TimeField" values ​​there, how can I calculate and display the time interval between these two values? Is it possible to do this with "def TimeInterval" like I did below?

Models.py:

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    dayOneStart = models.TimeField(null='true', blank='true', verbose_name='Начало дня')
    dayOneEnd = models.TimeField(null='true', blank='true', verbose_name='Конец дня')

def TimeInterval(self):
    return(self.dayOneEnd - self.dayOneStart)

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)


@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
maksam07, 2021-04-09
@H2CO3

I must say right away, store the signals elsewhere, read about it in the documentation
Essentially: https://stackoverflow.com/a/53816845

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question