pythondjangodjango-modelsdjango-formsdjango-1.8

Django: durationField default value


What's the right way to use Django's DurationField?

When I use time_passed = models.DurationField(default=0):

When I use time_passed = models.DurationField(default=timedelta()):

So what is the right way to use a default value on duration field or a workaround for this issue?


Solution

  • The default should be a timedelta. This is a bug in Django and is set to be fixed in the 1.8.1 release.

    See: https://code.djangoproject.com/ticket/24566

    So using default should be:

    from datetime import timedelta
    
    
    time_passed = models.DurationField(default=timedelta)