Refs #2443 -- Allowed creation of objects with NULL DurationFields

This commit is contained in:
Michał Modzelewski 2015-01-02 02:47:49 +01:00 committed by Tim Graham
parent 5a4ac4ead9
commit ee86e59051
3 changed files with 12 additions and 1 deletions

View File

@ -1651,6 +1651,8 @@ class DurationField(Field):
def get_db_prep_value(self, value, connection, prepared=False): def get_db_prep_value(self, value, connection, prepared=False):
if connection.features.has_native_duration_field: if connection.features.has_native_duration_field:
return value return value
if value is None:
return None
return value.total_seconds() * 1000000 return value.total_seconds() * 1000000
def get_db_converters(self, connection): def get_db_converters(self, connection):

View File

@ -129,6 +129,10 @@ class DurationModel(models.Model):
field = models.DurationField() field = models.DurationField()
class NullDurationModel(models.Model):
field = models.DurationField(null=True)
class PrimaryKeyCharModel(models.Model): class PrimaryKeyCharModel(models.Model):
string = models.CharField(max_length=10, primary_key=True) string = models.CharField(max_length=10, primary_key=True)

View File

@ -5,7 +5,7 @@ from django.core import exceptions, serializers
from django.db import models from django.db import models
from django.test import TestCase from django.test import TestCase
from .models import DurationModel from .models import DurationModel, NullDurationModel
class TestSaveLoad(TestCase): class TestSaveLoad(TestCase):
@ -16,6 +16,11 @@ class TestSaveLoad(TestCase):
loaded = DurationModel.objects.get() loaded = DurationModel.objects.get()
self.assertEqual(loaded.field, duration) 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): class TestQuerying(TestCase):