From 5c7ac7494ab5f1cdc364f829f61ffef85ad7344b Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Sun, 19 Jan 2014 09:35:59 -0500 Subject: [PATCH] Fixed #18907 -- Correct docs regard population of model backrefs. Thanks simonpercivall for the report and Aymeric for the patch. --- docs/topics/db/queries.txt | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/docs/topics/db/queries.txt b/docs/topics/db/queries.txt index 7f04c66fcb..94073d1a70 100644 --- a/docs/topics/db/queries.txt +++ b/docs/topics/db/queries.txt @@ -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 `. 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 ----------------------------