Ticket #3376: 3376_immutable_clean_dict.diff
File 3376_immutable_clean_dict.diff, 4.8 KB (added by , 18 years ago) |
---|
-
django/utils/datastructures.py
43 43 return True 44 44 return False 45 45 46 class ImmutableDict(dict): 47 "A dictionary whose values cannot be modified." 48 def __setitem__(self, key, value): 49 raise TypeError('This dictionary is immutable') 50 51 def __delitem__(self, key): 52 raise TypeError('This dictionary is immutable') 53 54 def update(self, d): 55 raise TypeError('This dictionary is immutable') 56 46 57 class SortedDict(dict): 47 58 "A dictionary that keeps its keys in the order in which they're inserted." 48 59 def __init__(self, data=None): -
django/newforms/forms.py
2 2 Form classes 3 3 """ 4 4 5 from django.utils.datastructures import SortedDict, MultiValueDict 5 from django.utils.datastructures import SortedDict, MultiValueDict, ImmutableDict 6 6 from django.utils.html import escape 7 7 from fields import Field 8 8 from widgets import TextInput, Textarea, HiddenInput, MultipleHiddenInput … … 187 187 errors[NON_FIELD_ERRORS] = e.messages 188 188 if errors: 189 189 delattr(self, 'clean_data') 190 else: 191 self.clean_data = ImmutableDict(self.clean_data) # Make it immutable. 190 192 self.__errors = errors 191 193 192 194 def clean(self): -
tests/regressiontests/forms/tests.py
72 72 >>> w.render('email', 'ŠĐĆŽćžšđ', attrs={'class': 'fun'}) 73 73 u'<input type="password" class="fun" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" name="email" />' 74 74 75 The render_value argument lets you specify whether the widget should render76 its value. You may want to do this for security reasons.77 >>> w = PasswordInput(render_value=True)78 >>> w.render('email', 'secret')79 u'<input type="password" name="email" value="secret" />'80 >>> w = PasswordInput(render_value=False)81 >>> w.render('email', '')82 u'<input type="password" name="email" />'83 >>> w.render('email', None)84 u'<input type="password" name="email" />'85 >>> w.render('email', 'secret')86 u'<input type="password" name="email" />'87 >>> w = PasswordInput(attrs={'class': 'fun'}, render_value=False)88 >>> w.render('email', 'secret')89 u'<input type="password" class="fun" name="email" />'90 91 75 # HiddenInput Widget ############################################################ 92 76 93 77 >>> w = HiddenInput() … … 1745 1729 ... 1746 1730 KeyError: "Key 'nonexistentfield' not found in Form" 1747 1731 1732 The clean_data dictionary is immutable -- i.e., you cannot add to it, delete 1733 from it or change its values. Use the copy() method to get a new dictionary 1734 whose values you can edit. 1735 >>> p.clean_data['first_name'] = 'foo' 1736 Traceback (most recent call last): 1737 ... 1738 TypeError: This dictionary is immutable 1739 >>> del p.clean_data['first_name'] 1740 Traceback (most recent call last): 1741 ... 1742 TypeError: This dictionary is immutable 1743 >>> p.clean_data['new_field_name'] = 'foo' 1744 Traceback (most recent call last): 1745 ... 1746 TypeError: This dictionary is immutable 1747 >>> new_clean_data = p.clean_data.copy() 1748 >>> new_clean_data['new_field_name'] = 'foo' 1749 1748 1750 >>> for boundfield in p: 1749 1751 ... print boundfield 1750 1752 <input type="text" name="first_name" value="John" id="id_first_name" /> -
docs/newforms.txt
273 273 >>> f.clean_data # Doesn't contain extra_field_1, etc. 274 274 {'cc_myself': True, 'message': u'Hi there', 'sender': u'foo@example.com', 'subject': u'hello'} 275 275 276 The ``clean_data`` dictionary is immutable -- you cannot add to it, delete from 277 it or change its values. Use the ``copy()`` method, or the ``dict()`` builtin 278 function, to get a new dictionary whose values you can edit:: 279 280 >>> f.clean_data['subject'] = 'foo' 281 Traceback (most recent call last): 282 ... 283 TypeError: This dictionary is immutable 284 >>> del f.clean_data['subject'] 285 Traceback (most recent call last): 286 ... 287 TypeError: This dictionary is immutable 288 >>> f.clean_data['new_field_name'] = 'foo' 289 Traceback (most recent call last): 290 ... 291 TypeError: This dictionary is immutable 292 293 # Method one: copy() 294 >>> new_clean_data = f.clean_data.copy() 295 >>> new_clean_data['new_field_name'] = 'foo' # Now it's mutable. 296 297 # Method two: dict() 298 >>> other_clean_data = dict(f.clean_data, new_field_name='foo') 299 276 300 Behavior of unbound forms 277 301 ~~~~~~~~~~~~~~~~~~~~~~~~~ 278 302