id summary reporter owner description type status component version severity resolution keywords cc stage has_patch needs_docs needs_tests needs_better_patch easy ui_ux 33143 Block import-time queries Adam Johnson Florian Zimmermann "It's possible to make ORM queries at import time. For example: {{{ class Form(forms.Form): country = forms.ChoiceField(choices=[c.name for c in Country.objects.all()]) }}} I see import time queries fairly frequently, and every time I have encountered them they were a mistake. They end up querying the database once when the web server starts and caching the data forever, which is never desired. Python allows imports to happen at any time, so there's no way to detect when ""import time"" is over, and the app is really running. An inner import may cause a module to be first loaded during a web request. I therefore propose we block queries until *after* the `AppConfig.ready()` phase. This would protect against most problems. We could block until *before* `AppConfig.ready()`. Queries inside a `ready()` method kind-of imply the user knows that the query is one-off at startup. But we do encourage users to perform some imports within `ready()` methods such as for signal registration." Cleanup/optimization closed Database layer (models, ORM) dev Normal fixed Ready for checkin 1 0 0 0 0 0