Field initial value callable should take a request object as an argument
|Reported by:||Mitar||Owned by:||nobody|
|Cc:||mmitar@…||Triage Stage:||Design decision needed|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Currently it is possible to define initial value for a field as a callable. But this is quite limited in use as it does not take any context in which it should run.
I am proposing that it should take a request object. In this way it is possible to set initial values based on a request. For example, based on GeoIP information. We should pass a request object to the form constructor which would then pass it along to the those callables.
I am attaching a short helper function which allows me that I define such callables already. It postprocesses the form class so that request is available to callables. So in the view it is necessary just to construct your form with:
form = initial_accepts_request(request, MyForm)()
It still allows initial values to be passed at construction time and they override callables.
But it would be much better if this would be officially supported.