Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Fixed #18709 -- Check if initial_value is a callable #246

Merged
merged 1 commit into from 11 months ago

4 participants

Jeroen Dekkers Florian Apolloner Kamil Gałuszka Marc Tamlyn
Jeroen Dekkers

In _get_changed_data, check if initial_value is a callable and call it
if it is.

Florian Apolloner
Owner

Could it be that you got the ticket number wrong?

Jeroen Dekkers

You're right, I just fixed the number. Somehow I wrote the wrong number in the commit message and didn't notice when doing the pull request. It should be 18709 as the branch name said.

Kamil Gałuszka

It will be great to merge that.

Jeroen Dekkers Fixed #18709 -- Check if initial_value is a callable
In _get_changed_data, check if initial_value is a callable and call it
if it is.
d0788c2
Jeroen Dekkers
dekkers commented May 21, 2013

I rebased to current master

Marc Tamlyn
Owner

Looks good to me.

Marc Tamlyn mjtamlyn merged commit adeec00 into from May 22, 2013
Marc Tamlyn mjtamlyn closed this May 22, 2013
Jeroen Dekkers dekkers deleted the branch May 22, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

May 22, 2013
Jeroen Dekkers Fixed #18709 -- Check if initial_value is a callable
In _get_changed_data, check if initial_value is a callable and call it
if it is.
d0788c2
This page is out of date. Refresh to see the latest.
2  django/forms/forms.py
@@ -342,6 +342,8 @@ def changed_data(self):
342 342
                 data_value = field.widget.value_from_datadict(self.data, self.files, prefixed_name)
343 343
                 if not field.show_hidden_initial:
344 344
                     initial_value = self.initial.get(name, field.initial)
  345
+                    if callable(initial_value):
  346
+                        initial_value = initial_value()
345 347
                 else:
346 348
                     initial_prefixed_name = self.add_initial_prefix(name)
347 349
                     hidden_widget = field.hidden_widget()
24  tests/forms_tests/tests/test_formsets.py
... ...
@@ -1,8 +1,10 @@
1 1
 # -*- coding: utf-8 -*-
2 2
 from __future__ import unicode_literals
3 3
 
  4
+import datetime
  5
+
4 6
 from django.forms import (CharField, DateField, FileField, Form, IntegerField,
5  
-    ValidationError, formsets)
  7
+    SplitDateTimeField, ValidationError, formsets)
6 8
 from django.forms.formsets import BaseFormSet, formset_factory
7 9
 from django.forms.util import ErrorList
8 10
 from django.test import TestCase
@@ -45,6 +47,13 @@ def clean(self):
45 47
     formset=BaseFavoriteDrinksFormSet, extra=3)
46 48
 
47 49
 
  50
+# Used in ``test_formset_splitdatetimefield``.
  51
+class SplitDateTimeForm(Form):
  52
+    when = SplitDateTimeField(initial=datetime.datetime.now)
  53
+
  54
+SplitDateTimeFormSet = formset_factory(SplitDateTimeForm)
  55
+
  56
+
48 57
 class FormsFormsetTestCase(TestCase):
49 58
     def test_basic_formset(self):
50 59
         # A FormSet constructor takes the same arguments as Form. Let's create a FormSet
@@ -882,6 +891,19 @@ def test_formset_nonzero(self):
882 891
         self.assertEqual(len(formset.forms), 0)
883 892
         self.assertTrue(formset)
884 893
 
  894
+    def test_formset_splitdatetimefield(self):
  895
+        """
  896
+        Formset should also work with SplitDateTimeField(initial=datetime.datetime.now).
  897
+        Regression test for #18709.
  898
+        """
  899
+        data = {
  900
+            'form-TOTAL_FORMS': '1',
  901
+            'form-INITIAL_FORMS': '0',
  902
+            'form-0-when_0': '1904-06-16',
  903
+            'form-0-when_1': '15:51:33',
  904
+        }
  905
+        formset = SplitDateTimeFormSet(data)
  906
+        self.assertTrue(formset.is_valid())
885 907
 
886 908
     def test_formset_error_class(self):
887 909
         # Regression tests for #16479 -- formsets form use ErrorList instead of supplied error_class
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.