There are a couple reasons I think users should be steered away from
`get_initial_for_field()`. One reason is that the API is simpler because
it doesn't require passing redundant `field` and `field_name` arguments.
Another reason is that going through `BoundField` has the advantage of
using the `BoundField` instance's cache, as `initial` is a cached
property. (I'm going to be filing another ticket related to the latter
difference, later.)
--
Ticket URL: <https://code.djangoproject.com/ticket/32933>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Old description:
> Currently, the documentation points users to
> `Form.get_initial_for_field(field, field_name)` to get the initial data
> for a form field. However, I think it would be better to point users to
> `BoundField.initial`, e.g. using the pattern `form[field_name].initial`.
> (`BoundField.initial` should also be added to the
> [https://docs.djangoproject.com/en/3.2/ref/forms/api/#attributes-of-
> boundfield BoundField attribute documentation], as it seems to have been
> left out.)
>
> There are a couple reasons I think users should be steered away from
> `get_initial_for_field()`. One reason is that the API is simpler because
> it doesn't require passing redundant `field` and `field_name` arguments.
> Another reason is that going through `BoundField` has the advantage of
> using the `BoundField` instance's cache, as `initial` is a cached
> property. (I'm going to be filing another ticket related to the latter
> difference, later.)
New description:
Currently, the documentation points users to
`Form.get_initial_for_field(field, field_name)` to get the initial data
for a form field. However, I think it would be better to point users to
`BoundField.initial`, e.g. using the pattern `form[field_name].initial`.
(`BoundField.initial` should also be added to the
[https://docs.djangoproject.com/en/3.2/ref/forms/api/#attributes-of-
boundfield BoundField attribute documentation], as it seems to have been
left out.)
There are a couple reasons I think users should be steered away from
`get_initial_for_field()`. One reason is that `BoundField'`s API is
simpler because it doesn't require passing redundant `field` and
`field_name` arguments. Another reason is that going through `BoundField`
has the advantage of using the `BoundField` instance's cache, as `initial`
is a cached property. (I'm going to be filing another ticket related to
the latter difference, later.)
--
--
Ticket URL: <https://code.djangoproject.com/ticket/32933#comment:1>
Old description:
> Currently, the documentation points users to
> `Form.get_initial_for_field(field, field_name)` to get the initial data
> for a form field. However, I think it would be better to point users to
> `BoundField.initial`, e.g. using the pattern `form[field_name].initial`.
> (`BoundField.initial` should also be added to the
> [https://docs.djangoproject.com/en/3.2/ref/forms/api/#attributes-of-
> boundfield BoundField attribute documentation], as it seems to have been
> left out.)
>
> There are a couple reasons I think users should be steered away from
> `get_initial_for_field()`. One reason is that `BoundField'`s API is
> simpler because it doesn't require passing redundant `field` and
> `field_name` arguments. Another reason is that going through `BoundField`
> has the advantage of using the `BoundField` instance's cache, as
> `initial` is a cached property. (I'm going to be filing another ticket
> related to the latter difference, later.)
New description:
Currently, the documentation
[https://docs.djangoproject.com/en/3.2/ref/forms/api/#django.forms.Form.get_initial_for_field
points users to] `Form.get_initial_for_field(field, field_name)` to get
the initial data for a form field. However, I think it would be better to
point users to `BoundField.initial`, e.g. using the pattern
`form[field_name].initial`. (`BoundField.initial` should also be added to
the [https://docs.djangoproject.com/en/3.2/ref/forms/api/#attributes-of-
boundfield BoundField attribute documentation], as it seems to have been
left out.)
There are a couple reasons I think users should be steered away from
`get_initial_for_field()`. One reason is that `BoundField'`s API is
simpler because it doesn't require passing redundant `field` and
`field_name` arguments. Another reason is that going through `BoundField`
has the advantage of using the `BoundField` instance's cache, as `initial`
is a cached property. (I'm going to be filing another ticket related to
the latter difference, later.)
--
--
Ticket URL: <https://code.djangoproject.com/ticket/32933#comment:2>
Old description:
> Currently, the documentation
> [https://docs.djangoproject.com/en/3.2/ref/forms/api/#django.forms.Form.get_initial_for_field
> points users to] `Form.get_initial_for_field(field, field_name)` to get
> the initial data for a form field. However, I think it would be better to
> point users to `BoundField.initial`, e.g. using the pattern
> `form[field_name].initial`. (`BoundField.initial` should also be added to
> the [https://docs.djangoproject.com/en/3.2/ref/forms/api/#attributes-of-
> boundfield BoundField attribute documentation], as it seems to have been
> left out.)
>
> There are a couple reasons I think users should be steered away from
> `get_initial_for_field()`. One reason is that `BoundField'`s API is
> simpler because it doesn't require passing redundant `field` and
> `field_name` arguments. Another reason is that going through `BoundField`
> has the advantage of using the `BoundField` instance's cache, as
> `initial` is a cached property. (I'm going to be filing another ticket
> related to the latter difference, later.)
New description:
Currently, the documentation
[https://docs.djangoproject.com/en/3.2/ref/forms/api/#django.forms.Form.get_initial_for_field
points users to] `Form.get_initial_for_field(field, field_name)` to get
the initial data for a form field. However, I think it would be better to
point users to `BoundField.initial`
([https://github.com/django/django/blob/7f33c1e22dbc34a7afae7967783725b10f1f13b1/django/forms/boundfield.py#L212-L220
link to code]), e.g. using the pattern `form[field_name].initial`.
(`BoundField.initial` should also be added to the
[https://docs.djangoproject.com/en/3.2/ref/forms/api/#attributes-of-
boundfield BoundField attribute documentation], as it seems to have been
left out.)
There are a couple reasons I think users should be steered away from
`get_initial_for_field()`. One reason is that `BoundField'`s API is
simpler because it doesn't require passing redundant `field` and
`field_name` arguments. Another reason is that going through `BoundField`
has the advantage of using the `BoundField` instance's cache, as `initial`
is a cached property. (I'm going to be filing another ticket related to
the latter difference, later.)
--
--
Ticket URL: <https://code.djangoproject.com/ticket/32933#comment:3>
* owner: nobody => Chris Jerdonek
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/32933#comment:4>
* has_patch: 0 => 1
Comment:
PR: https://github.com/django/django/pull/14651
--
Ticket URL: <https://code.djangoproject.com/ticket/32933#comment:5>
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/32933#comment:6>
Comment (by Carlton Gibson):
#32937 brought up that whilst Form.get_initial_for_field() doesn't cache
its return values, whereas BoundField.initial does.
Discussion there was to...
> ... add a paragraph in
https://docs.djangoproject.com/en/3.2/ref/forms/api/#dynamic-initial-
values saying _Why_ BoundField.initial is preferred: simpler interface,
and caches values, which matters for callables, like now().
That can be resolved as part of this ticket.
--
Ticket URL: <https://code.djangoproject.com/ticket/32933#comment:7>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32933#comment:8>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/32933#comment:9>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/32933#comment:10>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"6559a55eed09780b30ee3c3636ccf30318622bcf" 6559a55]:
{{{
#!CommitTicketReference repository=""
revision="6559a55eed09780b30ee3c3636ccf30318622bcf"
Fixed #32933 -- Documented BoundField.initial as preferred over
Form.get_initial_for_field().
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32933#comment:11>
Comment (by Carlton Gibson <carlton.gibson@…>):
In [changeset:"aace6c531d5b03b023516b244d87cf5608e24d90" aace6c53]:
{{{
#!CommitTicketReference repository=""
revision="aace6c531d5b03b023516b244d87cf5608e24d90"
[3.2.x] Fixed #32933 -- Documented BoundField.initial as preferred over
Form.get_initial_for_field().
Backport of 6559a55eed09780b30ee3c3636ccf30318622bcf from main
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32933#comment:12>