The method 'data', which is called by 'as_widget' ALWAYS used the default
widget of the field which leads to issues.
We recognized this issue when using 'show_hidden_initial=True' and widgets
for datetime objects.
The problem can also be observed when using BoundField.as_hidden as it
uses 'as_widget' as well.
cf.
as_widget:
https://github.com/django/django/blob/master/django/forms/forms.py#L526
data:
https://github.com/django/django/blob/master/django/forms/forms.py#L569
as_hidden:
https://github.com/django/django/blob/master/django/forms/forms.py#L562
--
Ticket URL: <https://code.djangoproject.com/ticket/24347>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Old description:
> 'as_widget' has the parameter 'widget' which renders the field. However,
> this parameter is not used all the time during the 'as_widget' call.
>
> The method 'data', which is called by 'as_widget' ALWAYS used the default
> widget of the field which leads to issues.
>
> We recognized this issue when using 'show_hidden_initial=True' and
> widgets for datetime objects.
>
> The problem can also be observed when using BoundField.as_hidden as it
> uses 'as_widget' as well.
>
> cf.
> as_widget:
> https://github.com/django/django/blob/master/django/forms/forms.py#L526
> data:
> https://github.com/django/django/blob/master/django/forms/forms.py#L569
> as_hidden:
> https://github.com/django/django/blob/master/django/forms/forms.py#L562
New description:
'as_widget' has the parameter 'widget' which renders the field. However,
this parameter is not used all the time during the 'as_widget' call.
The method 'data', which is called by 'as_widget' ALWAYS used the default
widget of the field which leads to issues like formatting etc.
We recognized this issue when using 'show_hidden_initial=True' and widgets
for datetime objects.
The problem can also be observed when using BoundField.as_hidden as it
uses 'as_widget' as well.
cf.
as_widget:
https://github.com/django/django/blob/master/django/forms/forms.py#L526
data:
https://github.com/django/django/blob/master/django/forms/forms.py#L569
as_hidden:
https://github.com/django/django/blob/master/django/forms/forms.py#L562
--
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:1>
Old description:
> 'as_widget' has the parameter 'widget' which renders the field. However,
> this parameter is not used all the time during the 'as_widget' call.
>
> The method 'data', which is called by 'as_widget' ALWAYS used the default
> widget of the field which leads to issues like formatting etc.
>
> We recognized this issue when using 'show_hidden_initial=True' and
> widgets for datetime objects.
>
> The problem can also be observed when using BoundField.as_hidden as it
> uses 'as_widget' as well.
>
> cf.
> as_widget:
> https://github.com/django/django/blob/master/django/forms/forms.py#L526
> data:
> https://github.com/django/django/blob/master/django/forms/forms.py#L569
> as_hidden:
> https://github.com/django/django/blob/master/django/forms/forms.py#L562
New description:
'as_widget' has the parameter 'widget' which renders the field. However,
this parameter is not used all the time during the 'as_widget' call.
The method 'data', which is (indirectly via 'value') called by 'as_widget'
ALWAYS used the default widget of the field which leads to issues like
formatting etc.
We recognized this issue when using 'show_hidden_initial=True' and widgets
for datetime objects.
The problem can also be observed when using BoundField.as_hidden as it
uses 'as_widget' as well.
cf.
as_widget:
https://github.com/django/django/blob/master/django/forms/forms.py#L526
data:
https://github.com/django/django/blob/master/django/forms/forms.py#L569
as_hidden:
https://github.com/django/django/blob/master/django/forms/forms.py#L562
--
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:2>
* version: master => 1.8alpha1
* needs_tests: 0 => 1
Old description:
> 'as_widget' has the parameter 'widget' which renders the field. However,
> this parameter is not used all the time during the 'as_widget' call.
>
> The method 'data', which is (indirectly via 'value') called by
> 'as_widget' ALWAYS used the default widget of the field which leads to
> issues like formatting etc.
>
> We recognized this issue when using 'show_hidden_initial=True' and
> widgets for datetime objects.
>
> The problem can also be observed when using BoundField.as_hidden as it
> uses 'as_widget' as well.
>
> cf.
> as_widget:
> https://github.com/django/django/blob/master/django/forms/forms.py#L526
> data:
> https://github.com/django/django/blob/master/django/forms/forms.py#L569
> as_hidden:
> https://github.com/django/django/blob/master/django/forms/forms.py#L562
New description:
'as_widget' has the parameter 'widget' which renders the field. However,
this parameter is not used all the time during the 'as_widget' call.
The method 'data', which is (indirectly via 'value') called by 'as_widget'
ALWAYS used the default widget of the field which leads to issues like
formatting etc.
We recognized this issue when using 'show_hidden_initial=True' and widgets
for datetime objects.
The problem can also be observed when using BoundField.as_hidden as it
uses 'as_widget' as well.
cf.
as_widget:
https://github.com/django/django/blob/master/django/forms/forms.py#L526
data:
https://github.com/django/django/blob/master/django/forms/forms.py#L569
as_hidden:
https://github.com/django/django/blob/master/django/forms/forms.py#L562
Versions: 1.4, 1.5, 1.6, 1.7, 1.8, master
--
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:3>
* status: new => closed
* resolution: => needsinfo
Comment:
Replying to [ticket:24347 srkunze]:
> 'as_widget' has the parameter 'widget' which renders the field. However,
this parameter is not used all the time during the 'as_widget' call.
Sorry, but that's not true. The widget parameter is used in
`widget.render(...)` at the end of the method. It's totally possible
something is not working properly, but then we'll need a more thorough
example to demonstrate the issue.
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:4>
Comment (by srkunze):
Replying to [comment:4 claudep]:
> Replying to [ticket:24347 srkunze]:
> > 'as_widget' has the parameter 'widget' which renders the field.
However, this parameter is not used all the time during the 'as_widget'
call.
>
> Sorry, but that's not true.
It is true.
> The widget parameter is used in `widget.render(...)` at the end of the
method.
I am not saying that this is the only possible place where the widget is
used.
> It's totally possible something is not working properly,
Please, carefully read my ticket again as I described what's wrong:
> The method 'data', which is (indirectly via 'value') called by
'as_widget' ALWAYS used the default widget of the field which leads to
issues like formatting etc.
> but then we'll need a more thorough example to demonstrate the issue.
Cf. my ticket again:
> The problem can also be observed when using BoundField.as_hidden as it
uses 'as_widget' as well.
`BouldField.as_hidden` should always use the HiddenWidget during
processing `BoundField.as_widget`, but `BouldField.data` uses the default
widget which is not the HiddenWidget of course.
A more concise presentation of the issue:
`BoundField.as_hidden` ---> `BoundField.as_widget` ---> `BoundField.value`
---> `BoundField.data`
"--->" means "uses"
`BoundField.as_hidden` specifies the HiddenWidget when it calls
`BoundField.as_widget`
`BoundField.value` and `BoundField.data` do not respect the specified
widget
Hence, the output is flawed.
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:5>
* status: closed => new
* resolution: needsinfo =>
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:6>
Old description:
> 'as_widget' has the parameter 'widget' which renders the field. However,
> this parameter is not used all the time during the 'as_widget' call.
>
> The method 'data', which is (indirectly via 'value') called by
> 'as_widget' ALWAYS used the default widget of the field which leads to
> issues like formatting etc.
>
> We recognized this issue when using 'show_hidden_initial=True' and
> widgets for datetime objects.
>
> The problem can also be observed when using BoundField.as_hidden as it
> uses 'as_widget' as well.
>
> cf.
> as_widget:
> https://github.com/django/django/blob/master/django/forms/forms.py#L526
> data:
> https://github.com/django/django/blob/master/django/forms/forms.py#L569
> as_hidden:
> https://github.com/django/django/blob/master/django/forms/forms.py#L562
>
> Versions: 1.4, 1.5, 1.6, 1.7, 1.8, master
New description:
'as_widget' has the parameter 'widget' which renders the field. However,
this parameter is not used all the time during the 'as_widget' call.
The method 'data', which is (indirectly via 'value') called by
'as_widget', ALWAYS uses the default widget of the field which leads to
issues like formatting etc.
We recognized this issue when using 'show_hidden_initial=True' and widgets
for datetime objects.
The problem can also be observed when using BoundField.as_hidden as it
uses 'as_widget' as well.
cf.
as_widget:
https://github.com/django/django/blob/master/django/forms/forms.py#L526
data:
https://github.com/django/django/blob/master/django/forms/forms.py#L569
as_hidden:
https://github.com/django/django/blob/master/django/forms/forms.py#L562
Versions: 1.4, 1.5, 1.6, 1.7, 1.8, master
--
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:7>
Old description:
> 'as_widget' has the parameter 'widget' which renders the field. However,
> this parameter is not used all the time during the 'as_widget' call.
>
> The method 'data', which is (indirectly via 'value') called by
> 'as_widget', ALWAYS uses the default widget of the field which leads to
> issues like formatting etc.
>
> We recognized this issue when using 'show_hidden_initial=True' and
> widgets for datetime objects.
>
> The problem can also be observed when using BoundField.as_hidden as it
> uses 'as_widget' as well.
>
> cf.
> as_widget:
> https://github.com/django/django/blob/master/django/forms/forms.py#L526
> data:
> https://github.com/django/django/blob/master/django/forms/forms.py#L569
> as_hidden:
> https://github.com/django/django/blob/master/django/forms/forms.py#L562
>
> Versions: 1.4, 1.5, 1.6, 1.7, 1.8, master
New description:
'as_widget' has the parameter 'widget' which renders the field. However,
this parameter is not used all the time during the 'as_widget' call.
The method 'data', which is (indirectly via 'value') called by
'as_widget', ALWAYS uses the default widget of the field which leads to
issues like formatting etc.
We recognized this issue when using 'show_hidden_initial=True' and widgets
for datetime objects.
The problem can also be observed when using BoundField.as_hidden as it
uses 'as_widget' as well.
cf.
as_widget:
https://github.com/django/django/blob/1.8a1/django/forms/forms.py#L566
data:
https://github.com/django/django/blob/1.8a1/django/forms/forms.py#L609
as_hidden:
https://github.com/django/django/blob/1.8a1/django/forms/forms.py#L602
Versions: 1.4, 1.5, 1.6, 1.7, 1.8, master
--
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:8>
Comment (by timgraham):
I'm also having trouble understanding your problem. It seems to me that
`BoundField.value/data` need to use the field's widget rather than
`HiddenInput` as the latter's `value_from_datadict()` doesn't know how to
handle each field's data. Can you offer a patch for this issue?
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:9>
* status: new => closed
* resolution: => needsinfo
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:10>
* status: closed => new
* version: 1.8alpha1 => 1.8beta1
* resolution: needsinfo =>
Comment:
> It seems to me that BoundField.value/data need to use the field's widget
rather than HiddenInput as the latter's value_from_datadict() doesn't know
how to handle each field's data.
It should use the HiddenInput all the time during the `as_hidden` call.
Otherwise, the data given into a form and the data returned (e.g. after a
POST) differs.
Widgets set with `hidden_initial=True` render their data two times: with
field.widget and with field.hidden_widget. Depending on whether or not its
a bound form, results in different formatting of the value/data.
This would be an example fix to show what I mean:
https://github.com/srkunze/django/commit/9480ce0c3853a47001bd12e339c263695c38978f
`hidden_initial=True` results in `only_initial=True`. That means the
'''hidden_initial parts''' of the POST data should not be changed.
PS:
Could somebody explain me the difference between '''data''' and
'''value''' with regards to BoundFields? Looks quite messy to me (e.g.
`BoundField.data` returns `value_from_datadict`; `prepare_value` gets
`data`, but it expects `value` according to its definition).
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:11>
Comment (by timgraham):
Thanks for the details. Unfortunately your patch doesn't pass the test
suite. Also, a new test showing the bug that's fixed is required. Will you
continue working on it?
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:12>
Comment (by srkunze):
You are welcome.
Before continue working on it, I would like to discuss whether my patch
makes sense and and I would like an answer on my PS.
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:13>
Comment (by timgraham):
Could you write a test case so I can better understand the problem?
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:14>
Comment (by srkunze):
I give it a try soon.
In the meantime, I would appreciate it if you could elaborate on this:
> Could somebody explain me the difference between '''data''' and
'''value''' with regards to BoundFields? Looks quite messy to me (e.g.
`BoundField.data` returns `value_from_datadict`; `prepare_value` gets
`data`, but it expects `value` according to its definition).
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:15>
Comment (by timgraham):
I think the docstrings explain the difference between `data` and `value`.
I wouldn't get caught up in the fact that `prepare_value()` is called with
a variable named `data` even though its parameter is named `value`.
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:16>
Comment (by srkunze):
I hope this illustrates our problem:
https://github.com/srkunze/django/commits/add_test_24347
In case of an error of the form (thus, the form needs to be re-rendered),
the hidden-initial should be re-rendered with the exact same value string
as it was rendered in unbound state.
Each different state of the form (corresponding to each written test):
unbound
bound + changed
bound + unchanged
shows a different behaviour (fails, fails, succeeds) which is unfortunate
for our internal testing library.
Replying to [comment:16 timgraham]:
> I think the docstrings explain the difference between `data` and
`value`. I wouldn't get caught up in the fact that `prepare_value()` is
called with a variable named `data` even though its parameter is named
`value`.
Unfortunately, docstring explains data by saying "data" and value by
saying "value". Thus, no relationship between data and value is defined
clearly.
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:17>
Comment (by timgraham):
I think datetimes maybe complicating the issue. It seems like at least one
of the issues is that for the following form:
{{{
class MyForm(forms.Form):
foo = forms.IntegerField(initial=1, show_hidden_initial=True)
form = MyForm({'foo': 5})
}}}
You expect the hidden input to be rendered with "1" instead of "5".
{{{
<label for="id_foo">Foo:</label>
<input id="id_foo" type="number" value="5" name="foo">
<input id="initial-id_foo" type="hidden" value="5" name="initial-foo">
}}}
Is that correct?
By the way, I tried looking at your tests, but they didn't pass even when
I uncommented "# uncomment to see changes with correctly formatted hidden-
initial" -- not sure if that was intended or not?
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:18>
Comment (by srkunze):
Replying to [comment:18 timgraham]:
> I think datetimes maybe complicating the issue.
Unfortunately, it is that complicated. Formatting (and its consistency
over the life-time of the form) is the issue here. That is why I chose
date inputs.
Replying to [comment:18 timgraham]:
> It seems like at least one of the issues is that for the following form:
> {{{
> class MyForm(forms.Form):
> foo = forms.IntegerField(initial=1, show_hidden_initial=True)
>
> form = MyForm({'foo': 5})
> }}}
Actually, it should look like this as browsers send `initial-foo` as well:
{{{
form = MyForm({'foo': 5, 'initial-foo': 1})
}}}
> You expect the hidden input to be rendered with "1" instead of "5".
> {{{
> <label for="id_foo">Foo:</label>
> <input id="id_foo" type="number" value="5" name="foo">
> <input id="initial-id_foo" type="hidden" value="5" name="initial-foo">
> }}}
> Is that correct?
Interesting. You accidentally found another error.
I created another commit and added more tests to cover all that.
> By the way, I tried looking at your tests, but they didn't pass even
when I uncommented "# uncomment to see changes with correctly formatted
hidden-initial" -- not sure if that was intended or not?
The comments were currently not intended to make the tests pass. They just
represent how **well-formed** POST data would look like IF the unbound
form would have produced correctly formatted hidden-initial data.
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:19>
Comment (by srkunze):
Here, we go:
https://github.com/srkunze/django/commit/c1fdbeeb19a41b2f3dcc09f501bd5ff9298b846d
14 tests now:
{{{
D1) FAILED test_boundfield__unbound_form__date
D2) FAILED test_boundfield__changed_bound_form__date
D3) FAILED test_boundfield__changed_bound_form__bogus_initial__date
D4) FAILED test_boundfield__changed_bound_form__missing_initial__date
D5) SUCCEEDED test_boundfield__unchanged_bound_form__date
D6) SUCCEEDED test_boundfield__unchanged_bound_form__missing_initial__date
I1) SUCCEEDED test_boundfield__unbound_form__integer
I2) FAILED test_boundfield__changed_bound_form__integer
I3) FAILED test_boundfield__changed_bound_form__bogus_initial__integer
I4) FAILED
test_boundfield__changed_bound_form__missing_initial__integer
I5) SUCCEEDED test_boundfield__unchanged_bound_form__integer
I6) SUCCEEDED
test_boundfield__unchanged_bound_form__missing_initial__integer
}}}
**unbound_form** - should be clear
**changed_bound_form** - form with data that has changed with regard to
initial
**unchanged_bound_form** - form with data that has not changed with regard
to initial
**bogus_initial** - user changed hidden initial
**missing_initial** - browser did not send hidden initial
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:20>
* needs_better_patch: 0 => 1
* has_patch: 0 => 1
* needs_tests: 1 => 0
* stage: Unreviewed => Accepted
Comment:
Well, I guess we can accept the ticket, but even after spending time
looking at it, I still don't have a good understanding of the problem(s)
or what a solution might look like.
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:21>
Comment (by srkunze):
> Well, I guess we can accept the ticket, but even after spending time
looking at it, I still don't have a good understanding of the problem(s)
I actually hoped you would be the one telling me. :D
> or what a solution might look like.
After our conversation and looking at the test-cases, I am even unsure if
my patch would touch the heart of the problem.
That is, btw., why I asked about the relationship between data and value.
However, as it seems the problem lies even deeper.
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:22>
* keywords: BoundField, as_widget => BoundField, as_widget, hidden,
initial, hidden_initial
* needs_docs: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:23>
* version: 1.8beta1 => master
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:24>
* status: new => closed
* resolution: => wontfix
--
Ticket URL: <https://code.djangoproject.com/ticket/24347#comment:25>