#21045: BoundField.value doesn't use to_python ----------------------------+-------------------- Reporter: Elvard | Owner: nobody Type: Bug | Status: new Component: Forms | Version: 1.5 Severity: Normal | Keywords: Triage Stage: Unreviewed | Has patch: 0 Easy pickings: 0 | UI/UX: 0 ----------------------------+-------------------- The problem occures when using data from POST in bound form:
{{{ class CustomForm(forms.Form): field = forms.IntegerField(initial=42) form = CustomForm() print(type(form['field'].value())) # int form = CustomForm({'field': '42'}) print(type(form['field'].value())) # str }}} `form['field']` is `IntegerField`, but it returns string, when you pass data from POST/GET (which are usually strings). It even returns string, when you pass `initial='42'` (which is odd also). The problem is with FKs in `ModelForm`: the value in unbound form is int (from database), but in bound form it's string (from POST). I've checked source code and `BoundField.value` uses `Field.prepare_value`, which is undocumented and (except for `DateTimeField`) it just returns original value. I would expect, that `value` is always data type defined by `Field`, which is done in `to_python` method. Questions are: 1) Is it bug or feature? 2) Should I use `to_python` instead, when I want "consistent" type of field value? 3) What's the meaning of `prepare_value`? -- Ticket URL: <https://code.djangoproject.com/ticket/21045> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/049.df653ff2f788d55978750d8ecc3c15b7%40djangoproject.com. For more options, visit https://groups.google.com/groups/opt_out.