Permalink
Browse files

Fixed #21829 -- Added default AppConfigs.

Thanks Russell for the report, Marc for the initial patch, Carl for the
final review, and everyone who contributed to the design discussion.
  • Loading branch information...
1 parent 29ddae7 commit 2ff93e027c7b35378cc450a926bc2e4a446cacf0 @aaugustin aaugustin committed Jan 24, 2014
Showing with 190 additions and 107 deletions.
  1. +42 −31 django/apps/base.py
  2. +1 −1 django/conf/project_template/project_name/settings.py
  3. +2 −3 django/contrib/admin/__init__.py
  4. +13 −2 django/contrib/admin/apps.py
  5. +1 −1 django/contrib/admin/sites.py
  6. +1 −0 django/contrib/admindocs/__init__.py
  7. +3 −6 django/contrib/auth/__init__.py
  8. +5 −0 django/contrib/auth/apps.py
  9. +5 −2 django/contrib/comments/__init__.py
  10. +1 −8 django/contrib/contenttypes/__init__.py
  11. +5 −1 django/contrib/contenttypes/apps.py
  12. +1 −0 django/contrib/flatpages/__init__.py
  13. +1 −0 django/contrib/formtools/__init__.py
  14. +3 −0 django/contrib/gis/__init__.py
  15. +1 −0 django/contrib/humanize/__init__.py
  16. +3 −0 django/contrib/messages/__init__.py
  17. +1 −0 django/contrib/redirects/__init__.py
  18. +1 −0 django/contrib/sessions/__init__.py
  19. +3 −0 django/contrib/sitemaps/__init__.py
  20. +1 −0 django/contrib/sites/__init__.py
  21. +1 −0 django/contrib/staticfiles/__init__.py
  22. +1 −0 django/contrib/syndication/__init__.py
  23. +1 −0 django/contrib/webdesign/__init__.py
  24. +2 −1 django/core/checks/registry.py
  25. +1 −8 docs/intro/tutorial01.txt
  26. +28 −7 docs/ref/applications.txt
  27. +32 −19 docs/ref/contrib/admin/index.txt
  28. +1 −1 docs/ref/contrib/gis/tutorial.txt
  29. +10 −7 docs/releases/1.7.txt
  30. +2 −3 docs/topics/auth/default.txt
  31. +1 −1 tests/admin_scripts/tests.py
  32. +1 −0 tests/apps/default_config_app/__init__.py
  33. +5 −0 tests/apps/default_config_app/apps.py
  34. +6 −0 tests/apps/tests.py
  35. +1 −1 tests/i18n/tests.py
  36. +3 −4 tests/runtests.py
View
@@ -61,53 +61,64 @@ def create(cls, entry):
Factory that creates an app config from an entry in INSTALLED_APPS.
"""
try:
- # If import_module succeeds, entry is a path to an app module.
+ # If import_module succeeds, entry is a path to an app module,
+ # which may specify an app config class with default_app_config.
# Otherwise, entry is a path to an app config class or an error.
module = import_module(entry)
except ImportError:
- # Avoid django.utils.module_loading.import_by_path because it
- # masks errors -- it reraises ImportError as ImproperlyConfigured.
mod_path, _, cls_name = entry.rpartition('.')
# Raise the original exception when entry cannot be a path to an
# app config class.
if not mod_path:
raise
- mod = import_module(mod_path)
- try:
- cls = getattr(mod, cls_name)
- except AttributeError:
- # Emulate the error that "from <mod_path> import <cls_name>"
- # would raise when <mod_path> exists but not <cls_name>, with
- # more context (Python just says "cannot import name ...").
- raise ImportError(
- "cannot import name '%s' from '%s'" % (cls_name, mod_path))
-
- # Check for obvious errors. (This check prevents duck typing, but
- # it could be removed if it became a problem in practice.)
- if not issubclass(cls, AppConfig):
- raise ImproperlyConfigured(
- "'%s' isn't a subclass of AppConfig." % entry)
-
- # Obtain app name here rather than in AppClass.__init__ to keep
- # all error checking for entries in INSTALLED_APPS in one place.
+ else:
try:
- app_name = cls.name
+ # If this works, the app module specifies an app config class.
+ entry = module.default_app_config
except AttributeError:
- raise ImproperlyConfigured(
- "'%s' must supply a name attribute." % entry)
+ # Otherwise, it simply uses the default app config class.
+ return cls(entry, module)
+ else:
+ mod_path, _, cls_name = entry.rpartition('.')
+
+ # If we're reaching this point, we must load the app config class
+ # located at <mod_path>.<cls_name>.
- # Ensure app_name points to a valid module.
- app_module = import_module(app_name)
+ # Avoid django.utils.module_loading.import_by_path because it
+ # masks errors -- it reraises ImportError as ImproperlyConfigured.
+ mod = import_module(mod_path)
+ try:
+ cls = getattr(mod, cls_name)
+ except AttributeError:
+ # Emulate the error that "from <mod_path> import <cls_name>"
+ # would raise when <mod_path> exists but not <cls_name>, with
+ # more context (Python just says "cannot import name ...").
+ raise ImportError(
+ "cannot import name '%s' from '%s'" % (cls_name, mod_path))
+
+ # Check for obvious errors. (This check prevents duck typing, but
+ # it could be removed if it became a problem in practice.)
+ if not issubclass(cls, AppConfig):
+ raise ImproperlyConfigured(
+ "'%s' isn't a subclass of AppConfig." % entry)
+
+ # Obtain app name here rather than in AppClass.__init__ to keep
+ # all error checking for entries in INSTALLED_APPS in one place.
+ try:
+ app_name = cls.name
+ except AttributeError:
+ raise ImproperlyConfigured(
+ "'%s' must supply a name attribute." % entry)
- # Entry is a path to an app config class.
- return cls(app_name, app_module)
+ # Ensure app_name points to a valid module.
+ app_module = import_module(app_name)
+
+ # Entry is a path to an app config class.
+ return cls(app_name, app_module)
- else:
- # Entry is a path to an app module.
- return cls(entry, module)
def get_model(self, model_name):
"""
@@ -30,7 +30,7 @@
# Application definition
INSTALLED_APPS = (
- 'django.contrib.admin.apps.AdminConfig',
+ 'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
@@ -1,6 +1,5 @@
# ACTION_CHECKBOX_NAME is unused, but should stay since its import from here
# has been referenced in documentation.
-from django.contrib.admin.checks import check_admin_app
from django.contrib.admin.decorators import register
from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
from django.contrib.admin.options import ModelAdmin, HORIZONTAL, VERTICAL
@@ -9,7 +8,6 @@
FieldListFilter, BooleanFieldListFilter, RelatedFieldListFilter,
ChoicesFieldListFilter, DateFieldListFilter, AllValuesFieldListFilter)
from django.contrib.admin.sites import AdminSite, site
-from django.core import checks
from django.utils.module_loading import autodiscover_modules
__all__ = [
@@ -24,4 +22,5 @@
def autodiscover():
autodiscover_modules('admin', register_to=site)
-checks.register('admin')(check_admin_app)
+
+default_app_config = 'django.contrib.admin.apps.AdminConfig'
@@ -1,11 +1,22 @@
from django.apps import AppConfig
-
+from django.core import checks
+from django.contrib.admin.checks import check_admin_app
from django.utils.translation import ugettext_lazy as _
-class AdminConfig(AppConfig):
+class SimpleAdminConfig(AppConfig):
+ """Simple AppConfig which does not do automatic discovery."""
+
name = 'django.contrib.admin'
verbose_name = _("administration")
def ready(self):
+ checks.register('admin')(check_admin_app)
+
+
+class AdminConfig(SimpleAdminConfig):
+ """The default AppConfig for admin which does autodiscovery."""
+
+ def ready(self):
+ super(AdminConfig, self).ready()
self.module.autodiscover()
@@ -161,7 +161,7 @@ def check_dependencies(self):
installed, as well as the auth context processor.
"""
if not apps.is_installed('django.contrib.admin'):
- raise ImproperlyConfigured("Put 'django.contrib.admin.apps.AdminConfig' in "
+ raise ImproperlyConfigured("Put 'django.contrib.admin' in "
"your INSTALLED_APPS setting in order to use the admin application.")
if not apps.is_installed('django.contrib.contenttypes'):
raise ImproperlyConfigured("Put 'django.contrib.contenttypes' in "
@@ -0,0 +1 @@
+default_app_config = 'django.contrib.admindocs.apps.AdminDocsConfig'
@@ -2,8 +2,6 @@
import re
from django.conf import settings
-from django.contrib.auth.checks import check_user_model
-from django.core import checks
from django.core.exceptions import ImproperlyConfigured, PermissionDenied
from django.utils.module_loading import import_by_path
from django.middleware.csrf import rotate_token
@@ -15,10 +13,6 @@
REDIRECT_FIELD_NAME = 'next'
-# Register the user model checks
-checks.register('models')(check_user_model)
-
-
def load_backend(path):
return import_by_path(path)()
@@ -164,3 +158,6 @@ def get_permission_codename(action, opts):
Returns the codename of the permission for the specified action.
"""
return '%s_%s' % (action, opts.model_name)
+
+
+default_app_config = 'django.contrib.auth.apps.AuthConfig'
@@ -1,8 +1,13 @@
from django.apps import AppConfig
+from django.core import checks
+from django.contrib.auth.checks import check_user_model
from django.utils.translation import ugettext_lazy as _
class AuthConfig(AppConfig):
name = 'django.contrib.auth'
verbose_name = _("authentication and authorization")
+
+ def ready(self):
+ checks.register('models')(check_user_model)
@@ -1,6 +1,6 @@
from importlib import import_module
import warnings
-from django.apps import apps
+from django.apps import apps as django_apps
from django.conf import settings
from django.core import urlresolvers
from django.core.exceptions import ImproperlyConfigured
@@ -16,7 +16,7 @@ def get_comment_app():
Get the comment app (i.e. "django.contrib.comments") as defined in the settings
"""
try:
- app_config = apps.get_app_config(get_comment_app_name().rpartition(".")[2])
+ app_config = django_apps.get_app_config(get_comment_app_name().rpartition(".")[2])
except LookupError:
raise ImproperlyConfigured("The COMMENTS_APP (%r) "
"must be in INSTALLED_APPS" % settings.COMMENTS_APP)
@@ -85,3 +85,6 @@ def get_approve_url(comment):
else:
return urlresolvers.reverse("django.contrib.comments.views.moderation.approve",
args=(comment.id,))
+
+
+default_app_config = 'django.contrib.comments.apps.CommentsConfig'
@@ -1,8 +1 @@
-# -*- coding: utf-8 -*-
-from __future__ import unicode_literals
-
-from django.contrib.contenttypes.checks import check_generic_foreign_keys
-from django.core import checks
-
-
-checks.register('models')(check_generic_foreign_keys)
+default_app_config = 'django.contrib.contenttypes.apps.ContentTypesConfig'
@@ -1,8 +1,12 @@
from django.apps import AppConfig
-
+from django.contrib.contenttypes.checks import check_generic_foreign_keys
+from django.core import checks
from django.utils.translation import ugettext_lazy as _
class ContentTypesConfig(AppConfig):
name = 'django.contrib.contenttypes'
verbose_name = _("content types")
+
+ def ready(self):
+ checks.register('models')(check_generic_foreign_keys)
@@ -0,0 +1 @@
+default_app_config = 'django.contrib.flatpages.apps.FlatPagesConfig'
@@ -0,0 +1 @@
+default_app_config = 'django.contrib.formtools.apps.FormToolsConfig'
@@ -4,3 +4,6 @@
memoryview = memoryview
else:
memoryview = buffer
+
+
+default_app_config = 'django.contrib.gis.apps.GISConfig'
@@ -0,0 +1 @@
+default_app_config = 'django.contrib.humanize.apps.HumanizeConfig'
@@ -1,2 +1,5 @@
from django.contrib.messages.api import * # NOQA
from django.contrib.messages.constants import * # NOQA
+
+
+default_app_config = 'django.contrib.messages.apps.MessagesConfig'
@@ -0,0 +1 @@
+default_app_config = 'django.contrib.redirects.apps.RedirectsConfig'
@@ -0,0 +1 @@
+default_app_config = 'django.contrib.sessions.apps.SessionsConfig'
@@ -133,3 +133,6 @@ def lastmod(self, item):
if self.date_field is not None:
return getattr(item, self.date_field)
return None
+
+
+default_app_config = 'django.contrib.sitemaps.apps.SiteMapsConfig'
@@ -0,0 +1 @@
+default_app_config = 'django.contrib.sites.apps.SitesConfig'
@@ -0,0 +1 @@
+default_app_config = 'django.contrib.staticfiles.apps.StaticFilesConfig'
@@ -0,0 +1 @@
+default_app_config = 'django.contrib.syndication.apps.SyndicationConfig'
@@ -0,0 +1 @@
+default_app_config = 'django.contrib.webdesign.apps.WebDesignConfig'
@@ -29,7 +29,8 @@ def my_check(apps, **kwargs):
def inner(check):
check.tags = tags
- self.registered_checks.append(check)
+ if check not in self.registered_checks:
+ self.registered_checks.append(check)
return check
return inner
@@ -435,7 +435,7 @@ look like this:
:filename: mysite/settings.py
INSTALLED_APPS = (
- 'django.contrib.admin.apps.AdminConfig',
+ 'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
@@ -444,13 +444,6 @@ look like this:
'polls',
)
-.. admonition:: Doesn't match what you see?
-
- If you're seeing ``'django.contrib.admin'`` instead of
- ``'django.contrib.admin.apps.AdminConfig'``, you're probably using a
- version of Django that doesn't match this tutorial version. You'll want
- to either switch to the older tutorial or the newer Django version.
-
Now Django knows to include the ``polls`` app. Let's run another command:
.. code-block:: bash
@@ -49,9 +49,15 @@ Configuring applications
To configure an application, subclass :class:`~django.apps.AppConfig` and put
the dotted path to that subclass in :setting:`INSTALLED_APPS`.
-Django uses the default :class:`~django.apps.AppConfig` class when
-:setting:`INSTALLED_APPS` simply contains the dotted path to an application
-module.
+When :setting:`INSTALLED_APPS` simply contains the dotted path to an
+application module, Django checks for a ``default_app_config`` variable in
+that module.
+
+If it's defined, it's the dotted path to the :class:`~django.apps.AppConfig`
+subclass for that application.
+
+If there is no ``default_app_config``, Django uses the base
+:class:`~django.apps.AppConfig` class.
For application authors
-----------------------
@@ -67,8 +73,23 @@ would provide a proper name for the admin::
name = 'rock_n_roll'
verbose_name = "Rock ’n’ roll"
-You would then tell your users to add ``'rock_n_roll.apps.RockNRollConfig'``
-to their :setting:`INSTALLED_APPS`.
+You can make your application load this :class:`~django.apps.AppConfig`
+subclass by default as follows::
+
+ # rock_n_roll/__init__.py
+
+ default_app_config = 'rock_n_roll.apps.RockNRollConfig'
+
+That will cause ``RockNRollConfig`` to be used when :setting:`INSTALLED_APPS`
+just contains ``'rock_n_roll'``. This allows you to make use of
+:class:`~django.apps.AppConfig` features without requiring your users to
+update their :setting:`INSTALLED_APPS` setting.
+
+Of course, you can also tell your users to put
+``'rock_n_roll.apps.RockNRollConfig'`` in their :setting:`INSTALLED_APPS`
+setting. You can even provide several different
+:class:`~django.apps.AppConfig` subclasses with different behaviors and allow
+your users to choose one via their :setting:`INSTALLED_APPS` setting.
The recommended convention is to put the configuration class in a submodule of
the application called ``apps``. However, this isn't enforced by Django.
@@ -87,7 +108,7 @@ configuration::
# anthology/apps.py
- from rock_n_roll.app import RockNRollConfig
+ from rock_n_roll.apps import RockNRollConfig
class GypsyJazzConfig(RockNRollConfig):
verbose_name = "Gypsy jazz"
@@ -213,7 +234,7 @@ Application registry
.. method:: apps.is_installed(app_name)
Checks whether an application with the given name exists in the registry.
- ``app_name`` is the full name of the app, e.g. 'django.contrib.admin'.
+ ``app_name`` is the full name of the app, e.g. ``'django.contrib.admin'``.
Unlike :meth:`~django.apps.apps.get_app_config`, this method can be called
safely at import time. If the registry is still being populated, it may
Oops, something went wrong.

0 comments on commit 2ff93e0

Please sign in to comment.