Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#32933 closed Cleanup/optimization (fixed)

Point users to BoundField.initial instead of Form.get_initial_for_field()

Reported by: Chris Jerdonek Owned by: Chris Jerdonek
Component: Documentation Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Chris Jerdonek)

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 (link to code), e.g. using the pattern form[field_name].initial. (BoundField.initial should also be added to the 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.)

Change History (12)

comment:1 by Chris Jerdonek, 3 years ago

Description: modified (diff)

comment:2 by Chris Jerdonek, 3 years ago

Description: modified (diff)

comment:3 by Chris Jerdonek, 3 years ago

Description: modified (diff)

comment:4 by Chris Jerdonek, 3 years ago

Owner: changed from nobody to Chris Jerdonek
Status: newassigned

comment:6 by Carlton Gibson, 3 years ago

Triage Stage: UnreviewedAccepted

comment:7 by Carlton Gibson, 3 years ago

#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.

comment:8 by Chris Jerdonek, 3 years ago

Patch needs improvement: set

comment:9 by Chris Jerdonek, 3 years ago

Patch needs improvement: unset

comment:10 by Carlton Gibson, 3 years ago

Triage Stage: AcceptedReady for checkin

comment:11 by Carlton Gibson <carlton.gibson@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In 6559a55:

Fixed #32933 -- Documented BoundField.initial as preferred over Form.get_initial_for_field().

comment:12 by Carlton Gibson <carlton.gibson@…>, 3 years ago

In aace6c53:

[3.2.x] Fixed #32933 -- Documented BoundField.initial as preferred over Form.get_initial_for_field().

Backport of 6559a55eed09780b30ee3c3636ccf30318622bcf from main

Note: See TracTickets for help on using tickets.
Back to Top