Thursday, March 31, 2016

Re: Why doesn't {{ form.field.value }} return cleaned data?



On 31.03.2016 01:08, Ryan Prater wrote:
Say I have a TypedMultipleChoiceField and I'm building a custom select multiple widget in my template.

If I want to check a box based on an initial value (ie. default checked options), OR check a box based on form data that has been returned (ie. error state), I would do this:

<input type="checkbox" {% if form.field.value %}checked{% endif %}>

HOWEVER, {{form.field.value}} source documentation (boundfield.py) states: "Returns the value for this BoundField, using the initial value if the form is not bound or the data otherwise."

So basically the equivalent of:
return (form.field.data or form.field.initial)


Not really, since data that are falsey may be valid data. So it's not equvalent to that.

Wouldn't it be more appropriate and useful for boundfield.value to return CLEANED data that has gone through to_python()? As in:
return (form.field.cleaned_data or form.field.initial)

No because then you would lose non-cleaned data in case of errors.

Also there is no guarantee that cleaned up data is valid when re-evaluated.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/62a1b22e-3d8b-4241-bbf7-20145686ab33%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment