#33143: Block import-time queries
-------------------------------------+-------------------------------------
               Reporter:  Adam       |          Owner:  nobody
  Johnson                            |
                   Type:             |         Status:  new
  Cleanup/optimization               |
              Component:  Database   |        Version:  dev
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 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.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33143>
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 django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/053.2b82b45f514f1627ee1bf7309e628cdc%40djangoproject.com.

Reply via email to