lucasrf27
lucasrf27

Reputation: 360

DurationField or Timedelta

im having a problem trying to set a duration in my to-do tasks.

i've tried with DurationField and some people told me to try the timedelta in your forms.py but im not quite shure how to pass the difference like (6days) from my two model DateField (start and end).

Models.py

from django.db import models
from datetime import datetime, timedelta



class To_do (models.Model):
    task = models.CharField(max_length=150)
    topic = models.CharField(max_length=150)
    how = models.TextField(max_length=600)
    start = models.DateField(default=datetime.today)
    end = models.DateField(blank=False)
    duration = models.DurationField(default=timedelta)

i'd like to display the difference for the user and after set an alarm for less than 3 days etc.

How do I solve this?

Upvotes: 0

Views: 737

Answers (1)

nigel222
nigel222

Reputation: 8192

The difference between two date or date/time values is a timedelta.

delta_time = end - start

Or if you need to code a delta-time constant from other numbers

from datetime import timedelta

my_delta = timedelta( days=3, hours=12, minutes=1 ) # half a week plus a minute

Don't use timedelta as the name as the default value if you are importing it! If what you mean to do is to pass a callable to calculate a timedelta, define a function to do that as above, and pass it as the default value.

Upvotes: 1

Related Questions