#22838: ModelChoiceField.cache_choices
---------------------------------------+------------------------
               Reporter:  mjtamlyn     |          Owner:  nobody
                   Type:  New feature  |         Status:  new
              Component:  Forms        |        Version:  master
               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            |
---------------------------------------+------------------------
 `ModelChoiceField` (and `MultipleModelChoiceField`) take an option
 `cache_choices`. This was introduced as a backwards compatibility effort
 in February 2007 (ee96c7eb2dea86e5bdaf93f7afa91b6f0128dd72) when the
 fields were made to not always cache their choices (#3534).

 The option is undocumented, untested and unused within Django.

 There are two possible courses of action here.

 Firstly, we could simply remove the option. It should have a deprecation
 cycle - in particular for people with custom subclasses of
 `ModelChoiceField` who pass in the option to the super.

 The alternative course of action which I prefer is to harden this into an
 actual feature. It already works as follows:
 - Create the `ModelChoiceField` with `cache_choices=True`.
 - The `ModelChoiceIterator` will cache its results on the
 `ModelChoiceField` and use that cache.

 What would be needed to make this a fully fledged feature in my opinion:
 - An API to clear the cache (`ModelChoiceField.clear_cache()`)
 - [Very possibly] Plug it in to the cache framework properly
 - Tests
 - Documentation

 I think this would be a very useful feature, and one which I have had use
 cases for often. The most obvious use cases is when working with the same
 form multiple times on a page (either as part of a formset or not) the
 same query is run for every form. I can call
 `MyForm.fields['modelchoicefield'].clear_cache()` after all the forms are
 rendered.

 The biggest possible issue here is that the iterator is not used until the
 form is rendered. We may need to change when the iterator is evaluated for
 this case.

 I may be able to provide a patch if the feature is accepted.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/22838>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.ae6c9824b51d58bd86cc39958bac0696%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to