{{{
if form.is_valid():
subject = form.cleaned_data['subject']
}}}
First, is_valid needs to be called, and it is not obvious from the naming
of the method and the fields that this is what populates cleaned_data.
Second, the ORM gives us regular fields rather than dictionary entries
accessed as strings.
I suggest a cleaner interface (first iteration):
{{{
if form.is_valid():
form.cleaning()
subject = form.subject
# to switch back to the original, use form.cleaning(False)
}}}
The fields could be defined as getter methods (@property), but since the
form is a custom user class, it may be easier to just swap the values upon
calling cleaning.
cleaning(False) is probably a rare operation.
The advantage of this is that client code is less likely to access the
original data in lieu of the cleaned data.
Also, is_valid does not suggest that anything but a check occurs. To
avoid the extra call to cleaning, one could do this:
{{{
if form.validate():
subject = form.subject
# to switch back to the original, use form.cleaning(False)
}}}
The change would be backward compatible, as is_valid() and .cleaned_data
work as before.
--
Ticket URL: <https://code.djangoproject.com/ticket/20569>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/20569#comment:1>
* cc: davidswelt (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/20569#comment:2>
Comment (by wim@…):
+1 I agree that validate is a more proper name than is_valid,
-0 but I am not in favor of dropping or replacing the cleaned_data
dictionary.
--
Ticket URL: <https://code.djangoproject.com/ticket/20569#comment:3>
* needs_better_patch: 0 => 1
* component: Forms => contrib.admin
* needs_tests: 0 => 1
* keywords: => 1
* needs_docs: 0 => 1
* has_patch: 0 => 1
* ui_ux: 0 => 1
Comment:
1
--
Ticket URL: <https://code.djangoproject.com/ticket/20569#comment:4>
* owner: nobody => anonymous
* status: new => assigned
Comment:
1
--
Ticket URL: <https://code.djangoproject.com/ticket/20569#comment:5>
Comment (by ogpcludi <sample@…>):
1
--
Ticket URL: <https://code.djangoproject.com/ticket/20569#comment:6>
* needs_better_patch: 0 => 1
* type: Uncategorized => Cleanup/optimization
* needs_tests: 0 => 1
* needs_docs: 0 => 1
Comment:
I believe this is going to require a design decision.
--
Ticket URL: <https://code.djangoproject.com/ticket/20569#comment:4>
* status: new => closed
* resolution: => invalid
Comment:
This would introduce massive backwards incompatibility, due to the
ambiguity of this in a template:
{{{
form.subject
}}}
This currently becomes `form['subject']` and accesses the bound field, but
with this change that would be shadowed by the attribute.
This is just asking for trouble, and an additional method to swap things
back is going to make things even more complicated and error prone.
Overall I think this is a worse API, especially once you've taken
templates into account, but either way the cost of the change far
outweighs any benefits from making it.
Therefore closing INVALID.
--
Ticket URL: <https://code.djangoproject.com/ticket/20569#comment:5>