Opened 19 years ago
Last modified 19 years ago
#3376 closed
newforms.Form.clean_data is not read-only — at Version 5
| Reported by: | Owned by: | Adrian Holovaty | |
|---|---|---|---|
| Component: | Forms | Version: | dev |
| Severity: | Keywords: | ||
| Cc: | Triage Stage: | Design decision needed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Is it possible to make newforms.Form.clean_data immutable? I'm kind of surprised it isn't, but I don't know the meaning or purpose of all things.
$ python manage.py shell
Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02)
[GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django import newforms
>>> class form(newforms.Form):
... a = newforms.IntegerField()
... b = newforms.CharField()
...
>>> d = {'a': 1, 'b': 'field b'}
>>> f = form(d)
>>> f.is_valid()
True
>>> f.clean_data
{'a': 1, 'b': u'field b'}
>>> f.clean_data['a'] = 2
>>> f.clean_data
{'a': 2, 'b': u'field b'}
Change History (5)
comment:1 by , 19 years ago
comment:2 by , 19 years ago
Hmm, I could go either way on this. Making clean_data an immutable dictionary would introduce a tiny bit of overhead, as we'd have to define our own ImmutableDict class. Do you really think having clean_data be mutable is a huge problem?
comment:3 by , 19 years ago
| Triage Stage: | Unreviewed → Design decision needed |
|---|
comment:4 by , 19 years ago
I supposed that validated form data would have been treated in the same read-only manner as request.GET and request.POST and also considering the following paragraph from http://www.djangoproject.com/documentation/newforms/.
If you have a bound Form instance and want to change the data somehow, or if you want to bind an unbound Form instance to some data, create another Form instance. There is no way to change data in a Form instance. Once a Form instance has been created, you should consider its data immutable, whether it has data or not.
It seemed contradictory to me to have form data be considered immutable but then be able to modify it because the clean_data dict is not read-only.
Here's my example again, unwikified, or wikified depending on how you think.
$ python manage.py shell Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02) [GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from django import newforms >>> class form(newforms.Form): ... a = newforms.IntegerField() ... b = newforms.CharField() ... >>> d = {'a': 1, 'b': 'field b'} >>> f = form(d) >>> f.is_valid() True >>> f.clean_data {'a': 1, 'b': u'field b'} >>> f.clean_data['a'] = 2 >>> f.clean_data {'a': 2, 'b': u'field b'} >>>