[Django] #32937: Form.get_initial_for_field() can return different values if called more than once

70 views
Skip to first unread message

Django

unread,
Jul 16, 2021, 9:57:05 AM7/16/21
to django-...@googlegroups.com
#32937: Form.get_initial_for_field() can return different values if called more
than once
------------------------------------------+------------------------
Reporter: Chris Jerdonek | Owner: nobody
Type: Uncategorized | Status: new
Component: Forms | 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,
[https://github.com/django/django/blob/304f6ff46a9d78aab0213b599356d3c7875b0bb9/django/forms/forms.py#L470-L478
Form.get_initial_for_field()] is
[https://docs.djangoproject.com/en/3.2/ref/forms/api/#django.forms.Form.get_initial_for_field
documented] as a way to retrieve the initial data for a form field.
However, it has the problem that it doesn't cache its return values, like
[https://github.com/django/django/blob/304f6ff46a9d78aab0213b599356d3c7875b0bb9/django/forms/boundfield.py#L229-L237
BoundField.initial] does, which can lead to buggy behavior e.g. in the
case of callables that return different values each time.

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.

Django

unread,
Jul 16, 2021, 10:08:20 AM7/16/21
to django-...@googlegroups.com
#32937: Form.get_initial_for_field() can return different values if called more
than once
--------------------------------+--------------------------------------

Reporter: Chris Jerdonek | Owner: nobody
Type: Uncategorized | Status: new
Component: Forms | 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
--------------------------------+--------------------------------------

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>

Django

unread,
Jul 16, 2021, 10:20:09 AM7/16/21
to django-...@googlegroups.com
#32937: Form.get_initial_for_field() can return different values if called more
than once
--------------------------------+--------------------------------------

Reporter: Chris Jerdonek | Owner: nobody
Type: Uncategorized | Status: new
Component: Forms | 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
--------------------------------+--------------------------------------

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>

Django

unread,
Jul 16, 2021, 10:49:02 AM7/16/21
to django-...@googlegroups.com
#32937: Form.get_initial_for_field() can return different values if called more
than once
--------------------------------+--------------------------------------

Reporter: Chris Jerdonek | Owner: nobody
Type: Uncategorized | Status: closed
Component: Forms | Version: dev
Severity: Normal | Resolution: duplicate

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 Carlton Gibson):

* status: new => closed
* resolution: => duplicate


Comment:

Agreed. Thanks.

Duplicate of #32933

--
Ticket URL: <https://code.djangoproject.com/ticket/32937#comment:3>

Reply all
Reply to author
Forward
0 new messages