#31295: required ModelChoiceField makes duplicate (cursor) queries to the
database
-------------------------------------+-------------------------------------
Reporter: Aurélien Pardon | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Forms | Version: 2.2
Severity: Normal | Resolution: needsinfo
Keywords: Model | 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):
> How is that correct if the queries yield different results ?
I don't know what else I can say. It's part of the design of
ModelChoiceField that it re-evaluates it's queryset each time it's
iterated.
See #3534 for example, 13 years ago:
> ModelChoiceField and ModelMultipleChoiceField cache the output of their
queryset the first time self.choices is accessed. This is bad for long-
running processes, such as mod_python, because the cache gets stale. Plus,
it's bad saving all of those choices in memory. The attached unit tests
illustrate the problem.
The exact same memory usage considerations were the motivation for the
move to iterator() in #23623. I can't see that being removed.
If the kind of race conditions you're talking about are a factor for your
app then, at least until #22841 is addressed, maybe you do need a
ModelChoiceIterator subclass for your field. (Always use the QuerySet
rather than the iterator...)
--
Ticket URL: <https://code.djangoproject.com/ticket/31295#comment:6>
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/065.17eb8bba0800b04d95b1025b40345ef3%40djangoproject.com.