Fixed #18907 -- Correct docs regard population of model backrefs.

Thanks simonpercivall for the report and Aymeric for the patch.
This commit is contained in:
Tim Graham 2014-01-19 09:35:59 -05:00
parent 66f546b906
commit 5c7ac7494a
1 changed files with 10 additions and 5 deletions

View File

@ -1280,11 +1280,16 @@ relationship on one end.
But how is this possible, given that a model class doesn't know which other
model classes are related to it until those other model classes are loaded?
The answer lies in the :setting:`INSTALLED_APPS` setting. The first time any
model is loaded, Django iterates over every model in :setting:`INSTALLED_APPS`
and creates the backward relationships in memory as needed. Essentially, one of
the functions of :setting:`INSTALLED_APPS` is to tell Django the entire model
domain.
The answer lies in the :data:`app registry <django.apps.apps>`. When Django
starts, it imports each application listed in :setting:`INSTALLED_APPS`, and
then the ``models`` module inside each application. Whenever a new model class
is created, Django adds backward-relationships to any related models. If the
related models haven't been imported yet, Django keeps tracks of the
relationships and adds them when the related models eventually are imported.
For this reason, it's particularly important that all the models you're using
be defined in applications listed in :setting:`INSTALLED_APPS`. Otherwise,
backwards relations may not work properly.
Queries over related objects
----------------------------