Fixed #21478 -- Corrected docs for when Field.db_type() is called.
Fixed #21643 -- repeated execution of qs with F() + timedelta

Thanks Tim Graham for review.
Added a context manager to hold the import lock.
Refactored registration of models.

Got rid of AppConfig._stub. As a side effect, app_cache.app_configs now
only contains entries for applications that are in INSTALLED_APPS, which
is a good thing and will allow dramatic simplifications (which I will
perform in the next commit). That required adjusting all methods that
iterate on app_configs without checking the "installed" flag, hence the
large changes in get_model[s].

Introduced AppCache.all_models to store models:
- while the app cache is being populated and a suitable app config
  object to register models isn't available yet;
- for applications that aren't in INSTALLED_APPS since they don't have
  an app config any longer.

Replaced get_model(seed_cache=False) by registered_model() which can be
kept simple and safe to call at any time, and removed the seed_cache
argument to get_model[s]. There's no replacement for that private API.

Allowed non-master app caches to go through populate() as it is now
safe to do so. They were introduced in 1.7 so backwards compatibility
isn't a concern as long as the migrations framework keeps working.
Removed the only_installed argument of get_app_config[s].

It wasn't used anywhere and couldn't be implemented any more since
non-installed apps no longer have a configuration.
Removed the app_config.installed flag.

Since applications that aren't installed no longer have an application
configuration, it is now always True in practice.

Provided an abstraction to temporarily add or remove applications as
several tests messed with app_config.installed to achieve this effect.
For now this API is _-prefixed because it looks dangerous.
Implemented two-stage app-cache population.

First stage imports app modules. It doesn't catch import errors. This
matches the previous behavior and keeps the code simple.

Second stage import models modules. It catches import errors and retries
them after walking through the entire list once. This matches the
previous behavior and seems useful.

populate_models() is intended to be equivalent to populate(). It isn't
wired yet. That is coming in the next commit.
Terminated AppCache._populate() with extreme prejudice.

It was called _populate() before I renamed it to populate(). Since it
has been superseded by populate_models() there's no reason to keep it.

Removed the can_postpone argument of load_app() as it was only used by
populate(). It's a private API and there's no replacement. Simplified
load_app() accordingly. Then new version behaves exactly like the old
one even though it's much shorter.
Added an _-prefix to pending lookups because it's transient.
Deprecated load_app().

Adjusted several tests that used it to add apps to the app cache and
then attempted to remove them by manipulating attributes directly.

Also renamed invalid_models to invalid_models_tests to avoid clashing
application labels between the outer and the inner invalid_models
applications.
Populated models only when necessary in get_app_config[s].

Took this opportunity to change get_app[s] to only consider applications
containing a model module as that seems slightly more backwards compatible.

Since callers that care about models pass only_with_models_module=True,
this has very few consequences. Only AppCommand needed a change.
Not all Python modules have a __path__.
Stopped iterating on INSTALLED_APPS.

Used the app cache's get_app_configs() method instead.
Renamed registered_model to has_model.

This avoids possible confusion with register_model.

Thanks Marc Tamlyn for the suggestion.
Stop testing for inclusion in INSTALLED_APPS.

Removed some exception masking in the comments app that was harmful and
couldn't be preserved easily.
Expurged INSTALLED_APPS from code and tests.

Except the app cache code and a few specific tests, of course.
Improved set_available_apps() in several ways.

- Tested consistency the current app_configs instead of INSTALLED_APPS.
- Considered applications added with _with_app as available.
- Added docstrings.
Complained on override_settings(INSTALLED_APPS=...).

Currently such overrides aren't reflected in the app cache.

It would  be possible to handle them. But that doesn't look like a very
good API. It makes it complicated to express "add this app" and "remove
this app", which are the most common operations on INSTALLED_APPS.
Added the ability to supply custom app configs.
Moved apps back in the toplevel django namespace.

Reverted 4a56a93cc458e9ab4dcab95d9f5067d4975dd1a2.