Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: durationField default value

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?

like image 420
MatZeg Avatar asked Apr 17 '15 12:04

MatZeg


1 Answers

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)
like image 106
MatZeg Avatar answered Oct 17 '22 17:10

MatZeg