From 2d7c27d3870e57edd1b2ac46b49f0a8804753a1e Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Mon, 9 Feb 2015 08:15:26 -0500 Subject: [PATCH] Fixed #24302 -- Added DurationField.formfield() --- django/db/models/fields/__init__.py | 7 +++++++ tests/model_fields/test_durationfield.py | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 5e11a47a68..cf5b6d7ef7 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -1689,6 +1689,13 @@ class DurationField(Field): val = self._get_val_from_obj(obj) return '' if val is None else duration_string(val) + def formfield(self, **kwargs): + defaults = { + 'form_class': forms.DurationField, + } + defaults.update(kwargs) + return super(DurationField, self).formfield(**defaults) + class EmailField(CharField): default_validators = [validators.validate_email] diff --git a/tests/model_fields/test_durationfield.py b/tests/model_fields/test_durationfield.py index 0ef160104d..fece35d57b 100644 --- a/tests/model_fields/test_durationfield.py +++ b/tests/model_fields/test_durationfield.py @@ -1,6 +1,7 @@ import datetime import json +from django import forms from django.core import exceptions, serializers from django.db import models from django.test import TestCase @@ -70,3 +71,11 @@ class TestValidation(TestCase): "'not a datetime' value has an invalid format. " "It must be in [DD] [HH:[MM:]]ss[.uuuuuu] format." ) + + +class TestFormField(TestCase): + # Tests for forms.DurationField are in the forms_tests app. + + def test_formfield(self): + field = models.DurationField() + self.assertIsInstance(field.formfield(), forms.DurationField)