Fixed #16138 -- Made FormMixin get_initial return a copy of the 'initial' 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
This commit is contained in:
Claude Paroz 2012-03-17 22:31:03 +00:00
parent f13328f776
commit c7cc4cfb9e
5 changed files with 27 additions and 4 deletions

View File

@ -19,7 +19,7 @@ class FormMixin(object):
""" """
Returns the initial data to use for forms on this view. Returns the initial data to use for forms on this view.
""" """
return self.initial return self.initial.copy()
def get_form_class(self): def get_form_class(self):
""" """

View File

@ -431,9 +431,14 @@ FormMixin
.. method:: get_initial() .. 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`. :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() .. method:: get_form_class()
Retrieve the form class to instantiate. By default Retrieve the form class to instantiate. By default

View File

@ -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 Python-Markdown library less than 2.1, a warning is issued that the output is
insecure. 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 Features deprecated in 1.4
========================== ==========================

View File

@ -5,11 +5,20 @@ from django.core.urlresolvers import reverse
from django import forms from django import forms
from django.test import TestCase from django.test import TestCase
from django.utils.unittest import expectedFailure from django.utils.unittest import expectedFailure
from django.views.generic.edit import FormMixin
from . import views from . import views
from .models import Artist, Author 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): class ModelFormMixinTests(TestCase):
def test_get_form(self): def test_get_form(self):
form_class = views.AuthorGetQuerySetFormView().get_form_class() form_class = views.AuthorGetQuerySetFormView().get_form_class()

View File

@ -5,6 +5,6 @@ from .dates import (ArchiveIndexViewTests, YearArchiveViewTests,
MonthArchiveViewTests, WeekArchiveViewTests, DayArchiveViewTests, MonthArchiveViewTests, WeekArchiveViewTests, DayArchiveViewTests,
DateDetailViewTests) DateDetailViewTests)
from .detail import DetailViewTest from .detail import DetailViewTest
from .edit import (ModelFormMixinTests, CreateViewTests, UpdateViewTests, from .edit import (FormMixinTests, ModelFormMixinTests, CreateViewTests,
DeleteViewTests) UpdateViewTests, DeleteViewTests)
from .list import ListViewTests from .list import ListViewTests