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

29 views
Skip to first unread message

Ryan Prater

unread,
Mar 30, 2016, 6:08:28 PM3/30/16
to Django users
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)

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)

Jani Tiainen

unread,
Mar 31, 2016, 7:40:42 AM3/31/16
to django...@googlegroups.com


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...@googlegroups.com.
To post to this group, send email to django...@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.

Reply all
Reply to author
Forward
0 new messages