Opened 3 years ago

Closed 3 years ago

#32924 closed Cleanup/optimization (fixed)

Move special-case logic in BoundField.initial() to BaseForm.get_initial_for_field()

Reported by: Chris Jerdonek Owned by: Chris Jerdonek
Component: Forms 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

This is another follow-up to ticket #32920.

Currently, BoundField.initial() has logic to special-case time and datetime objects:
https://github.com/django/django/blob/f5669fd7b568cf8a3eda1e65c1c6fb583c7b177d/django/forms/boundfield.py#L217-L219
I noticed that this logic can be moved to BaseForm.get_initial_for_field(), and in particular under the if callable(value) block:
https://github.com/django/django/blob/f5669fd7b568cf8a3eda1e65c1c6fb583c7b177d/django/forms/forms.py#L496-L497

Eventually, I think it could make sense to go further and move some of this logic to a new method of the Field class, which could permit the special-casing to be handled by overriding in sub-classes that use times and datetimes.

Change History (6)

comment:1 by Nick Pope, 3 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Carlton Gibson, 3 years ago

Triage Stage: AcceptedReady for checkin

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

In 469e769:

Refs #32924 -- Changed test_get_initial_for_field() to use subTests.

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

In 788441c:

Refs #32924 -- Changed test_initial_datetime_values() to use subTests.

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

Resolution: fixed
Status: assignedclosed

In 0dc25526:

Fixed #32924 -- Changed BaseForm.get_initial_for_field() to remove microseconds when needed.

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