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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.552292dbeb3674ab511aaa5b48df0e35%40djangoproject.com.

Reply via email to