Skip to content

Commit

Permalink
Refs #2443 -- Allowed creation of objects with NULL DurationFields
Browse files Browse the repository at this point in the history
  • Loading branch information
michalmo authored and timgraham committed Jan 7, 2015
1 parent 5a4ac4e commit ee86e59
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions django/db/models/fields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,8 @@ def to_python(self, value):
def get_db_prep_value(self, value, connection, prepared=False):
if connection.features.has_native_duration_field:
return value
if value is None:
return None
return value.total_seconds() * 1000000

def get_db_converters(self, connection):
Expand Down
4 changes: 4 additions & 0 deletions tests/model_fields/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ class DurationModel(models.Model):
field = models.DurationField()


class NullDurationModel(models.Model):
field = models.DurationField(null=True)


class PrimaryKeyCharModel(models.Model):
string = models.CharField(max_length=10, primary_key=True)

Expand Down
7 changes: 6 additions & 1 deletion tests/model_fields/test_durationfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.db import models
from django.test import TestCase

from .models import DurationModel
from .models import DurationModel, NullDurationModel


class TestSaveLoad(TestCase):
Expand All @@ -16,6 +16,11 @@ def test_simple_roundtrip(self):
loaded = DurationModel.objects.get()
self.assertEqual(loaded.field, duration)

def test_create_empty(self):
NullDurationModel.objects.create()
loaded = NullDurationModel.objects.get()
self.assertEqual(loaded.field, None)


class TestQuerying(TestCase):

Expand Down

0 comments on commit ee86e59

Please sign in to comment.