Add cleaned_form to supersede cleaned_data
|Reported by:||anonymous||Owned by:||nobody|
|Has patch:||no||Needs documentation:||yes|
|Needs tests:||yes||Patch needs improvement:||yes|
The cleaned_data dictionary is not very elegant:
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.
Change History (5)
comment:1 Changed 3 years ago by david.reitter@…
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
comment:4 Changed 3 years ago by Kamu
- Needs documentation set
- Needs tests set
- Patch needs improvement set
- Type changed from Uncategorized to Cleanup/optimization