Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #16138 -- Made FormMixin get_initial return a copy of the 'init…

…ial' class variable. Thanks hanson2010, wilfred@potatolondon.com and agriffis for their work on the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17765 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c7cc4cfb9ebef17930db4a92cd0632a536a4ff7a 1 parent f13328f
Claude Paroz authored March 17, 2012
2  django/views/generic/edit.py
@@ -19,7 +19,7 @@ def get_initial(self):
19 19
         """
20 20
         Returns the initial data to use for forms on this view.
21 21
         """
22  
-        return self.initial
  22
+        return self.initial.copy()
23 23
 
24 24
     def get_form_class(self):
25 25
         """
7  docs/ref/class-based-views.txt
@@ -431,9 +431,14 @@ FormMixin
431 431
 
432 432
     .. method:: get_initial()
433 433
 
434  
-        Retrieve initial data for the form. By default, returns
  434
+        Retrieve initial data for the form. By default, returns a copy of
435 435
         :attr:`.initial`.
436 436
 
  437
+    .. admonition:: Changed in 1.4
  438
+
  439
+        In Django 1.3, this method was returning the :attr:`initial` class
  440
+        variable itself.
  441
+
437 442
     .. method:: get_form_class()
438 443
 
439 444
         Retrieve the form class to instantiate. By default
9  docs/releases/1.4.txt
@@ -1107,6 +1107,15 @@ passed to the markdown filter, both the ``safe_mode=True`` and
1107 1107
 Python-Markdown library less than 2.1, a warning is issued that the output is
1108 1108
 insecure.
1109 1109
 
  1110
+FormMixin get_initial returns an instance-specific dictionary
  1111
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1112
+
  1113
+In Django 1.3, the ``get_initial`` method of the
  1114
+:class:`django.views.generic.edit.FormMixin` class was returning the
  1115
+class ``initial`` dictionary. This has been fixed to return a copy of this
  1116
+dictionary, so form instances can modify their initial data without messing
  1117
+with the class variable.
  1118
+
1110 1119
 Features deprecated in 1.4
1111 1120
 ==========================
1112 1121
 
9  tests/regressiontests/generic_views/edit.py
@@ -5,11 +5,20 @@
5 5
 from django import forms
6 6
 from django.test import TestCase
7 7
 from django.utils.unittest import expectedFailure
  8
+from django.views.generic.edit import FormMixin
8 9
 
9 10
 from . import views
10 11
 from .models import Artist, Author
11 12
 
12 13
 
  14
+class FormMixinTests(TestCase):
  15
+     def test_initial_data(self):
  16
+         """ Test instance independence of initial data dict (see #16138) """
  17
+         initial_1 = FormMixin().get_initial()
  18
+         initial_1['foo'] = 'bar'
  19
+         initial_2 = FormMixin().get_initial()
  20
+         self.assertNotEqual(initial_1, initial_2)
  21
+
13 22
 class ModelFormMixinTests(TestCase):
14 23
     def test_get_form(self):
15 24
         form_class = views.AuthorGetQuerySetFormView().get_form_class()
4  tests/regressiontests/generic_views/tests.py
@@ -5,6 +5,6 @@
5 5
     MonthArchiveViewTests, WeekArchiveViewTests, DayArchiveViewTests,
6 6
     DateDetailViewTests)
7 7
 from .detail import DetailViewTest
8  
-from .edit import (ModelFormMixinTests, CreateViewTests, UpdateViewTests,
9  
-    DeleteViewTests)
  8
+from .edit import (FormMixinTests, ModelFormMixinTests, CreateViewTests,
  9
+    UpdateViewTests, DeleteViewTests)
10 10
 from .list import ListViewTests

0 notes on commit c7cc4cf

Please sign in to comment.
Something went wrong with that request. Please try again.