diff --git a/django/apps/registry.py b/django/apps/registry.py index 41095bd41e..492a97f603 100644 --- a/django/apps/registry.py +++ b/django/apps/registry.py @@ -4,7 +4,7 @@ import sys import threading import warnings -from django.core.exceptions import ImproperlyConfigured +from django.core.exceptions import AppRegistryNotReady, ImproperlyConfigured from django.utils import lru_cache from django.utils.deprecation import RemovedInDjango19Warning from django.utils._os import upath @@ -116,7 +116,7 @@ class Apps(object): Raises an exception if the registry isn't ready. """ if not self.ready: - raise RuntimeError("App registry isn't ready yet.") + raise AppRegistryNotReady() def get_app_configs(self): """ diff --git a/django/core/exceptions.py b/django/core/exceptions.py index cf4aea0f36..552b0067c3 100644 --- a/django/core/exceptions.py +++ b/django/core/exceptions.py @@ -12,6 +12,11 @@ class DjangoRuntimeWarning(RuntimeWarning): pass +class AppRegistryNotReady(Exception): + """The django.apps registry is not populated yet""" + pass + + class ObjectDoesNotExist(Exception): """The requested object does not exist""" silent_variable_failure = True diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py index 0fe5ebd7ae..46f38042c1 100644 --- a/django/utils/translation/trans_real.py +++ b/django/utils/translation/trans_real.py @@ -10,6 +10,7 @@ from threading import local import warnings from django.apps import apps +from django.core.exceptions import AppRegistryNotReady from django.dispatch import receiver from django.test.signals import setting_changed from django.utils.deprecation import RemovedInDjango19Warning @@ -181,7 +182,14 @@ def translation(language): res.merge(t) return res - for app_config in reversed(list(apps.get_app_configs())): + try: + app_configs = reversed(list(apps.get_app_configs())) + except AppRegistryNotReady: + raise AppRegistryNotReady( + "The translation infrastructure cannot be initialized before the " + "apps registry is ready. Check that you don't make non-lazy " + "gettext calls at import time.") + for app_config in app_configs: apppath = os.path.join(app_config.path, 'locale') if os.path.isdir(apppath): res = _merge(apppath) diff --git a/docs/ref/applications.txt b/docs/ref/applications.txt index 8052524a49..f944a6fcac 100644 --- a/docs/ref/applications.txt +++ b/docs/ref/applications.txt @@ -382,9 +382,9 @@ Troubleshooting Here are some common problems that you may encounter during initialization: -* ``RuntimeError: App registry isn't ready yet.`` This happens when importing - an application configuration or a models module triggers code that depends - on the app registry. +* ``AppRegistryNotReady`` This happens when importing an application + configuration or a models module triggers code that depends on the app + registry. For example, :func:`~django.utils.translation.ugettext()` uses the app registry to look up translation catalogs in applications. To translate at diff --git a/docs/releases/1.7.txt b/docs/releases/1.7.txt index 3ab136d24b..a02bee3bcf 100644 --- a/docs/releases/1.7.txt +++ b/docs/releases/1.7.txt @@ -934,7 +934,7 @@ script with:: >>> import django >>> django.setup() -Otherwise, you will hit ``RuntimeError: App registry isn't ready yet.`` +Otherwise, you will hit an ``AppRegistryNotReady`` exception. App registry consistency ^^^^^^^^^^^^^^^^^^^^^^^^