[Django] #32933: Point users to BoundField.initial instead of Form.get_initial_for_field()

24 views
Skip to first unread message

Django

unread,
Jul 15, 2021, 2:37:54 PM7/15/21
to django-...@googlegroups.com
#32933: Point users to BoundField.initial instead of Form.get_initial_for_field()
------------------------------------------------+------------------------
Reporter: Chris Jerdonek | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: dev
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
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.)

--
Ticket URL: <https://code.djangoproject.com/ticket/32933>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 15, 2021, 2:39:19 PM7/15/21
to django-...@googlegroups.com
#32933: Point users to BoundField.initial instead of Form.get_initial_for_field()
-------------------------------------+-------------------------------------

Reporter: Chris Jerdonek | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Documentation | Version: dev
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Chris Jerdonek:

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>

Django

unread,
Jul 15, 2021, 2:41:10 PM7/15/21
to django-...@googlegroups.com
#32933: Point users to BoundField.initial instead of Form.get_initial_for_field()
-------------------------------------+-------------------------------------

Reporter: Chris Jerdonek | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Documentation | Version: dev
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Chris Jerdonek:

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>

Django

unread,
Jul 15, 2021, 2:45:49 PM7/15/21
to django-...@googlegroups.com
#32933: Point users to BoundField.initial instead of Form.get_initial_for_field()
-------------------------------------+-------------------------------------

Reporter: Chris Jerdonek | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Documentation | Version: dev
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Chris Jerdonek:

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:

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>

Django

unread,
Jul 15, 2021, 4:16:23 PM7/15/21
to django-...@googlegroups.com
#32933: Point users to BoundField.initial instead of Form.get_initial_for_field()
-------------------------------------+-------------------------------------
Reporter: Chris Jerdonek | Owner: Chris
Type: | Jerdonek
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Chris Jerdonek):

* owner: nobody => Chris Jerdonek
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/32933#comment:4>

Django

unread,
Jul 16, 2021, 8:04:13 AM7/16/21
to django-...@googlegroups.com
#32933: Point users to BoundField.initial instead of Form.get_initial_for_field()
-------------------------------------+-------------------------------------
Reporter: Chris Jerdonek | Owner: Chris
Type: | Jerdonek
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Chris Jerdonek):

* has_patch: 0 => 1


Comment:

PR: https://github.com/django/django/pull/14651

--
Ticket URL: <https://code.djangoproject.com/ticket/32933#comment:5>

Django

unread,
Jul 16, 2021, 9:29:28 AM7/16/21
to django-...@googlegroups.com
#32933: Point users to BoundField.initial instead of Form.get_initial_for_field()
-------------------------------------+-------------------------------------
Reporter: Chris Jerdonek | Owner: Chris
Type: | Jerdonek
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/32933#comment:6>

Django

unread,
Jul 16, 2021, 10:48:31 AM7/16/21
to django-...@googlegroups.com
#32933: Point users to BoundField.initial instead of Form.get_initial_for_field()
-------------------------------------+-------------------------------------
Reporter: Chris Jerdonek | Owner: Chris
Type: | Jerdonek
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Jul 16, 2021, 6:34:05 PM7/16/21
to django-...@googlegroups.com
#32933: Point users to BoundField.initial instead of Form.get_initial_for_field()
-------------------------------------+-------------------------------------
Reporter: Chris Jerdonek | Owner: Chris
Type: | Jerdonek
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Chris Jerdonek):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/32933#comment:8>

Django

unread,
Jul 21, 2021, 2:58:19 PM7/21/21
to django-...@googlegroups.com
#32933: Point users to BoundField.initial instead of Form.get_initial_for_field()
-------------------------------------+-------------------------------------
Reporter: Chris Jerdonek | Owner: Chris
Type: | Jerdonek
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Chris Jerdonek):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/32933#comment:9>

Django

unread,
Jul 27, 2021, 2:45:39 AM7/27/21
to django-...@googlegroups.com
#32933: Point users to BoundField.initial instead of Form.get_initial_for_field()
-------------------------------------+-------------------------------------
Reporter: Chris Jerdonek | Owner: Chris
Type: | Jerdonek
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/32933#comment:10>

Django

unread,
Jul 27, 2021, 11:59:30 AM7/27/21
to django-...@googlegroups.com
#32933: Point users to BoundField.initial instead of Form.get_initial_for_field()
-------------------------------------+-------------------------------------
Reporter: Chris Jerdonek | Owner: Chris
Type: | Jerdonek
Cleanup/optimization | Status: closed
Component: Documentation | Version: dev
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson <carlton.gibson@…>):

* 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>

Django

unread,
Jul 27, 2021, 11:59:49 AM7/27/21
to django-...@googlegroups.com
#32933: Point users to BoundField.initial instead of Form.get_initial_for_field()
-------------------------------------+-------------------------------------
Reporter: Chris Jerdonek | Owner: Chris
Type: | Jerdonek
Cleanup/optimization | Status: closed
Component: Documentation | Version: dev

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Reply all
Reply to author
Forward
0 new messages