Django: durationField default value

MatZeg picture MatZeg · Apr 17, 2015 · Viewed 7.3k times · Source

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

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

  • Migrations work
  • Form defaults don't work ('int' object has no attribute 'total_seconds')

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

  • Migrations don't work (ValueError: Cannot serialize: datetime.timedelta(0))
  • Form defaults work

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

Answer

MatZeg picture MatZeg · Apr 20, 2015

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: time_passed = models.DurationField(default=timedelta())