Merge pull request #246 from dekkers/ticket_18709

Fixed #18709 -- Check if initial_value is a callable
This commit is contained in:
Marc Tamlyn 2013-05-22 00:45:07 -07:00
commit adeec00979
2 changed files with 25 additions and 1 deletions

View File

@ -342,6 +342,8 @@ class BaseForm(object):
data_value = field.widget.value_from_datadict(self.data, self.files, prefixed_name) data_value = field.widget.value_from_datadict(self.data, self.files, prefixed_name)
if not field.show_hidden_initial: if not field.show_hidden_initial:
initial_value = self.initial.get(name, field.initial) initial_value = self.initial.get(name, field.initial)
if callable(initial_value):
initial_value = initial_value()
else: else:
initial_prefixed_name = self.add_initial_prefix(name) initial_prefixed_name = self.add_initial_prefix(name)
hidden_widget = field.hidden_widget() hidden_widget = field.hidden_widget()

View File

@ -1,8 +1,10 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
import datetime
from django.forms import (CharField, DateField, FileField, Form, IntegerField, from django.forms import (CharField, DateField, FileField, Form, IntegerField,
ValidationError, formsets) SplitDateTimeField, ValidationError, formsets)
from django.forms.formsets import BaseFormSet, formset_factory from django.forms.formsets import BaseFormSet, formset_factory
from django.forms.util import ErrorList from django.forms.util import ErrorList
from django.test import TestCase from django.test import TestCase
@ -45,6 +47,13 @@ FavoriteDrinksFormSet = formset_factory(FavoriteDrinkForm,
formset=BaseFavoriteDrinksFormSet, extra=3) formset=BaseFavoriteDrinksFormSet, extra=3)
# Used in ``test_formset_splitdatetimefield``.
class SplitDateTimeForm(Form):
when = SplitDateTimeField(initial=datetime.datetime.now)
SplitDateTimeFormSet = formset_factory(SplitDateTimeForm)
class FormsFormsetTestCase(TestCase): class FormsFormsetTestCase(TestCase):
def test_basic_formset(self): def test_basic_formset(self):
# A FormSet constructor takes the same arguments as Form. Let's create a FormSet # A FormSet constructor takes the same arguments as Form. Let's create a FormSet
@ -882,6 +891,19 @@ class FormsFormsetTestCase(TestCase):
self.assertEqual(len(formset.forms), 0) self.assertEqual(len(formset.forms), 0)
self.assertTrue(formset) self.assertTrue(formset)
def test_formset_splitdatetimefield(self):
"""
Formset should also work with SplitDateTimeField(initial=datetime.datetime.now).
Regression test for #18709.
"""
data = {
'form-TOTAL_FORMS': '1',
'form-INITIAL_FORMS': '0',
'form-0-when_0': '1904-06-16',
'form-0-when_1': '15:51:33',
}
formset = SplitDateTimeFormSet(data)
self.assertTrue(formset.is_valid())
def test_formset_error_class(self): def test_formset_error_class(self):
# Regression tests for #16479 -- formsets form use ErrorList instead of supplied error_class # Regression tests for #16479 -- formsets form use ErrorList instead of supplied error_class