On Mon, Apr 25, 2016 at 8:13 AM, <marcin.j.no...@gmail.com> wrote:

> The initialization process of models seems to be good step in cleaning
> many problems, but I don't understant why I can't import models.
> The usage of the model should raise exception when it is not
> loaded/initialized, but not importing a model class!
>

Django needs to maintain a registry of all the models found in all the
installed applications, and it needs each model to go into that registry
once and only once (you don't want a model being registered multiple
times). This requires there to be some period in time during which models
are essentially "locked", so that Django has the ability to build the model
registry without any other code interfering or causing duplicate copies of
model classes to be floating around from stray import statements. That
period occurs as Django is starting up, and until it's done you are not
permitted to do anything that imports a model class.

Typically the only way to actually run into a problem with this is to be
importing models inside the __init__.py file of an application, since that
causes an import at the time Django is loading and inspecting the
application. Avoiding imports of models at the top level of __init__.py is
the solution to this (and code which needs to get a list of all
applications/models should just use the API Django exposes for that rather
than trying to do all the imports itself; there's a reason why Django
provides introspection APIs for this).

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAL13Cg_nJ2cp9dgSAZ1RZvXX%2Bu%2BtbhdR2RHCi9XrVkyJpPaAEg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to