Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.1.X] Fixed #12048 - MultiWidget does not define `__deepcopy__`

  
Thanks to powderflask for report, test case and initial patch.

Backport of [12739] from trunk


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12740 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 693dcd90139a87462fc636c0b698743bfce677a0 1 parent 0a7ec91
@spookylukey spookylukey authored
View
5 django/forms/widgets.py
@@ -744,6 +744,11 @@ def _get_media(self):
media = media + w.media
return media
media = property(_get_media)
+
+ def __deepcopy__(self, memo):
+ obj = super(MultiWidget, self).__deepcopy__(memo)
+ obj.widgets = copy.deepcopy(self.widgets)
+ return obj
class SplitDateTimeWidget(MultiWidget):
"""
View
2  tests/regressiontests/forms/tests.py
@@ -32,6 +32,8 @@
from formsets import tests as formset_tests
from media import media_tests
+from widgets import WidgetTests
+
__test__ = {
'extra_tests': extra_tests,
'fields_tests': fields_tests,
View
40 tests/regressiontests/forms/widgets.py
@@ -1234,3 +1234,43 @@
"""
+
+from django.utils import copycompat as copy
+from unittest import TestCase
+from django import forms
+
+
+class SelectAndTextWidget(forms.MultiWidget):
+ """
+ MultiWidget subclass
+ """
+ def __init__(self, choices=[]):
+ widgets = [
+ forms.RadioSelect(choices=choices),
+ forms.TextInput
+ ]
+ super(SelectAndTextWidget, self).__init__(widgets)
+
+ def _set_choices(self, choices):
+ """
+ When choices are set for this widget, we want to pass those along to the Select widget
+ """
+ self.widgets[0].choices = choices
+ def _get_choices(self):
+ """
+ The choices for this widget are the Select widget's choices
+ """
+ return self.widgets[0].choices
+ choices = property(_get_choices, _set_choices)
+
+
+class WidgetTests(TestCase):
+
+ def test_12048(self):
+ # See ticket #12048.
+ w1 = SelectAndTextWidget(choices=[1,2,3])
+ w2 = copy.deepcopy(w1)
+ w2.choices = [4,5,6]
+ # w2 ought to be independent of w1, since MultiWidget ought
+ # to make a copy of its sub-widgets when it is copied.
+ self.assertEqual(w1.choices, [1,2,3])
Please sign in to comment.
Something went wrong with that request. Please try again.