One way to fix this would be for `get_initial_for_field()` to get its
value from `BoundField` when it needs to fall back to the field, which can
then cache it (similar to how `BoundField.initial` caches its return
value). The downside of this is that it adds another layer of indirection,
as `BoundField.initial` is already going to `Form.get_initial_for_field()`
for its value. Another option might be to deprecate
`get_initial_for_field()`, as I'm not sure if it provides any benefit that
an API on `BoundField` couldn't better provide.
See also #32933.
--
Ticket URL: <https://code.djangoproject.com/ticket/32937>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by Carlton Gibson):
Hey Chris.
Given #32933 — pointing users to `BoundField.initial`, would you object to
just documenting this?
We could 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()`.
What do you think? I'm not sure we get much benefit adding yet more code
to this one… 🤔
--
Ticket URL: <https://code.djangoproject.com/ticket/32937#comment:1>
Comment (by Chris Jerdonek):
Thanks, Carlton. Yes, something like that would be good and was along the
lines of what I was thinking. I wasn't sure which direction you'd want to
go until filing the ticket. So maybe this issue can be closed, and I can
add a comment to the doc ticket linking to this.
--
Ticket URL: <https://code.djangoproject.com/ticket/32937#comment:2>
* status: new => closed
* resolution: => duplicate
Comment:
Agreed. Thanks.
Duplicate of #32933
--
Ticket URL: <https://code.djangoproject.com/ticket/32937#comment:3>