{{{
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.
Comment (by Tim Graham):
[https://groups.google.com/g/django-developers/c/7JwWatLfP44/ django-
developers discussion]
--
Ticket URL: <https://code.djangoproject.com/ticket/33143#comment:1>
Comment (by Simon Charette):
I'd be in favor of blocking until *after* `AppConfig.ready()` phase as any
queries performed at `.ready()` time has the potential to crash due to
still unapplied migrations (missing table, schema mismatch) and can get
users in a pickle where they can't even run `migrate`.
[https://docs.djangoproject.com/en/3.2/ref/applications/#django.apps.AppConfig.ready
This is something we've been warning against for a while] and that I've
had to point to a fair amount of time when reviewing Django project
changes.
--
Ticket URL: <https://code.djangoproject.com/ticket/33143#comment:2>
Comment (by Adam Johnson):
Thanks for the developers discussion link Tim. I like the proposal of
warning and then if there are no serious reports, deprecating from the
next version. A `RuntimeWarning` sounds appropriate.
And I see you have some history with this Simon.
Aymeric was previously "+0" but he may have changed his mind. I will post
again on the thread with the link to this ticket just to see if there are
any dissenting opinions.
--
Ticket URL: <https://code.djangoproject.com/ticket/33143#comment:3>
* stage: Unreviewed => Accepted
Comment:
I think there's enough consensus to Accept.
Right or wrong, I suspect there are plenty of folks using `ready()` as a
home for running some kind of query at startup. We should have some advice
for them.
--
Ticket URL: <https://code.djangoproject.com/ticket/33143#comment:4>
* owner: nobody => Florian Zimmermann
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/33143#comment:5>
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/16923 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/33143#comment:6>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/33143#comment:7>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/33143#comment:8>
* needs_better_patch: 0 => 1
* needs_tests: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/33143#comment:9>
* needs_better_patch: 1 => 0
* needs_tests: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/33143#comment:10>
* needs_better_patch: 0 => 1
Comment:
Marking as need improvement per Adam's
[https://github.com/django/django/pull/16923#discussion_r1278985276
comment].
--
Ticket URL: <https://code.djangoproject.com/ticket/33143#comment:11>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/33143#comment:12>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/33143#comment:13>
* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/33143#comment:14>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"fbd16438f46bc2128926958ad24331da5d1b406f" fbd16438]:
{{{
#!CommitTicketReference repository=""
revision="fbd16438f46bc2128926958ad24331da5d1b406f"
Fixed #33143 -- Raised RuntimeWarning when performing import-time queries.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33143#comment:15>