diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py index 3cade522d4..d107e9a732 100644 --- a/django/views/generic/edit.py +++ b/django/views/generic/edit.py @@ -19,7 +19,7 @@ class FormMixin(object): """ Returns the initial data to use for forms on this view. """ - return self.initial + return self.initial.copy() def get_form_class(self): """ diff --git a/docs/ref/class-based-views.txt b/docs/ref/class-based-views.txt index e1ff28977d..5223aee018 100644 --- a/docs/ref/class-based-views.txt +++ b/docs/ref/class-based-views.txt @@ -431,9 +431,14 @@ FormMixin .. method:: get_initial() - Retrieve initial data for the form. By default, returns + Retrieve initial data for the form. By default, returns a copy of :attr:`.initial`. + .. admonition:: Changed in 1.4 + + In Django 1.3, this method was returning the :attr:`initial` class + variable itself. + .. method:: get_form_class() Retrieve the form class to instantiate. By default diff --git a/docs/releases/1.4.txt b/docs/releases/1.4.txt index d82ca03d28..211d1362c8 100644 --- a/docs/releases/1.4.txt +++ b/docs/releases/1.4.txt @@ -1107,6 +1107,15 @@ passed to the markdown filter, both the ``safe_mode=True`` and Python-Markdown library less than 2.1, a warning is issued that the output is insecure. +FormMixin get_initial returns an instance-specific dictionary +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +In Django 1.3, the ``get_initial`` method of the +:class:`django.views.generic.edit.FormMixin` class was returning the +class ``initial`` dictionary. This has been fixed to return a copy of this +dictionary, so form instances can modify their initial data without messing +with the class variable. + Features deprecated in 1.4 ========================== diff --git a/tests/regressiontests/generic_views/edit.py b/tests/regressiontests/generic_views/edit.py index 2bd982e3d9..651e14fff3 100644 --- a/tests/regressiontests/generic_views/edit.py +++ b/tests/regressiontests/generic_views/edit.py @@ -5,11 +5,20 @@ from django.core.urlresolvers import reverse from django import forms from django.test import TestCase from django.utils.unittest import expectedFailure +from django.views.generic.edit import FormMixin from . import views from .models import Artist, Author +class FormMixinTests(TestCase): + def test_initial_data(self): + """ Test instance independence of initial data dict (see #16138) """ + initial_1 = FormMixin().get_initial() + initial_1['foo'] = 'bar' + initial_2 = FormMixin().get_initial() + self.assertNotEqual(initial_1, initial_2) + class ModelFormMixinTests(TestCase): def test_get_form(self): form_class = views.AuthorGetQuerySetFormView().get_form_class() diff --git a/tests/regressiontests/generic_views/tests.py b/tests/regressiontests/generic_views/tests.py index d387216d41..72aab035a8 100644 --- a/tests/regressiontests/generic_views/tests.py +++ b/tests/regressiontests/generic_views/tests.py @@ -5,6 +5,6 @@ from .dates import (ArchiveIndexViewTests, YearArchiveViewTests, MonthArchiveViewTests, WeekArchiveViewTests, DayArchiveViewTests, DateDetailViewTests) from .detail import DetailViewTest -from .edit import (ModelFormMixinTests, CreateViewTests, UpdateViewTests, - DeleteViewTests) +from .edit import (FormMixinTests, ModelFormMixinTests, CreateViewTests, + UpdateViewTests, DeleteViewTests) from .list import ListViewTests