2013-12-24 22:40:12 +08:00
|
|
|
|
============
|
|
|
|
|
Applications
|
|
|
|
|
============
|
|
|
|
|
|
|
|
|
|
.. module:: django.apps
|
|
|
|
|
|
|
|
|
|
.. versionadded:: 1.7
|
|
|
|
|
|
|
|
|
|
Django contains a registry of installed applications that stores configuration
|
|
|
|
|
and provides introspection. It also maintains a list of available :doc:`models
|
|
|
|
|
</topics/db/models>`.
|
|
|
|
|
|
|
|
|
|
This registry is simply called :attr:`~django.apps.apps` and it's available in
|
|
|
|
|
:mod:`django.apps`::
|
|
|
|
|
|
|
|
|
|
>>> from django.apps import apps
|
|
|
|
|
>>> apps.get_app_config('admin').verbose_name
|
|
|
|
|
'Admin'
|
|
|
|
|
|
|
|
|
|
Projects and applications
|
|
|
|
|
=========================
|
|
|
|
|
|
|
|
|
|
Django has historically used the term **project** to describe an installation
|
|
|
|
|
of Django. A project is defined primarily by a settings module.
|
|
|
|
|
|
|
|
|
|
The term **application** describes a Python package that provides some set of
|
|
|
|
|
features. Applications may be reused in various projects.
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
This terminology is somewhat confusing these days as it became common to
|
|
|
|
|
use the phrase "web app" to describe what equates to a Django project.
|
|
|
|
|
|
|
|
|
|
Applications include some combination of models, views, templates, template
|
|
|
|
|
tags, static files, URLs, middleware, etc. They're generally wired into
|
|
|
|
|
projects with the :setting:`INSTALLED_APPS` setting and optionally with other
|
|
|
|
|
mechanisms such as URLconfs, the :setting:`MIDDLEWARE_CLASSES` setting, or
|
|
|
|
|
template inheritance.
|
|
|
|
|
|
|
|
|
|
It is important to understand that a Django application is just a set of code
|
|
|
|
|
that interacts with various parts of the framework. There's no such thing as
|
|
|
|
|
an ``Application`` object. However, there's a few places where Django needs to
|
|
|
|
|
interact with installed applications, mainly for configuration and also for
|
|
|
|
|
introspection. That's why the application registry maintains metadata in an
|
|
|
|
|
:class:`~django.apps.AppConfig` instance for each installed application.
|
|
|
|
|
|
|
|
|
|
Configuring applications
|
|
|
|
|
========================
|
|
|
|
|
|
|
|
|
|
To configure an application, subclass :class:`~django.apps.AppConfig` and put
|
|
|
|
|
the dotted path to that subclass in :setting:`INSTALLED_APPS`.
|
|
|
|
|
|
2014-01-25 05:43:00 +08:00
|
|
|
|
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.
|
2013-12-24 22:40:12 +08:00
|
|
|
|
|
|
|
|
|
For application authors
|
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
|
|
If you're creating a pluggable app called "Rock ’n’ roll", here's how you
|
|
|
|
|
would provide a proper name for the admin::
|
|
|
|
|
|
2014-01-03 06:06:25 +08:00
|
|
|
|
# rock_n_roll/apps.py
|
2013-12-24 22:40:12 +08:00
|
|
|
|
|
|
|
|
|
from django.apps import AppConfig
|
|
|
|
|
|
|
|
|
|
class RockNRollConfig(AppConfig):
|
|
|
|
|
name = 'rock_n_roll'
|
|
|
|
|
verbose_name = "Rock ’n’ roll"
|
|
|
|
|
|
2014-01-25 05:43:00 +08:00
|
|
|
|
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.
|
2013-12-24 22:40:12 +08:00
|
|
|
|
|
|
|
|
|
The recommended convention is to put the configuration class in a submodule of
|
2014-01-03 06:06:25 +08:00
|
|
|
|
the application called ``apps``. However, this isn't enforced by Django.
|
2013-12-24 22:40:12 +08:00
|
|
|
|
|
|
|
|
|
You must include the :attr:`~django.apps.AppConfig.name` attribute for Django
|
|
|
|
|
to determine which application this configuration applies to. You can define
|
|
|
|
|
any attributes documented in the :class:`~django.apps.AppConfig` API
|
|
|
|
|
reference.
|
|
|
|
|
|
2014-02-02 22:31:34 +08:00
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
|
If your code imports the application registry in an application's
|
|
|
|
|
``__init__.py``, the name ``apps`` will clash with the ``apps`` submodule.
|
|
|
|
|
The best practice is to move that code to a submodule and import it. A
|
|
|
|
|
workaround is to import the registry under a different name::
|
|
|
|
|
|
|
|
|
|
from django.apps import apps as django_apps
|
|
|
|
|
|
2013-12-24 22:40:12 +08:00
|
|
|
|
For application users
|
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
|
|
If you're using "Rock ’n’ roll" in a project called ``anthology``, but you
|
|
|
|
|
want it to show up as "Gypsy jazz" instead, you can provide your own
|
|
|
|
|
configuration::
|
|
|
|
|
|
|
|
|
|
# anthology/apps.py
|
|
|
|
|
|
2014-01-25 05:43:00 +08:00
|
|
|
|
from rock_n_roll.apps import RockNRollConfig
|
2013-12-24 22:40:12 +08:00
|
|
|
|
|
|
|
|
|
class GypsyJazzConfig(RockNRollConfig):
|
|
|
|
|
verbose_name = "Gypsy jazz"
|
|
|
|
|
|
|
|
|
|
# anthology/settings.py
|
|
|
|
|
|
|
|
|
|
INSTALLED_APPS = [
|
|
|
|
|
'anthology.apps.GypsyJazzConfig',
|
|
|
|
|
# ...
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
Again, defining project-specific configuration classes in a submodule called
|
|
|
|
|
``apps`` is a convention, not a requirement.
|
|
|
|
|
|
|
|
|
|
Application configuration
|
|
|
|
|
=========================
|
|
|
|
|
|
2013-12-26 04:57:52 +08:00
|
|
|
|
.. class:: AppConfig
|
2013-12-24 22:40:12 +08:00
|
|
|
|
|
|
|
|
|
Application configuration objects store metadata for an application. Some
|
|
|
|
|
attributes can be configured in :class:`~django.apps.AppConfig`
|
|
|
|
|
subclasses. Others are set by Django and read-only.
|
|
|
|
|
|
|
|
|
|
Configurable attributes
|
|
|
|
|
-----------------------
|
|
|
|
|
|
2013-12-31 23:23:42 +08:00
|
|
|
|
.. attribute:: AppConfig.name
|
2013-12-24 22:40:12 +08:00
|
|
|
|
|
2013-12-31 23:23:42 +08:00
|
|
|
|
Full Python path to the application, e.g. ``'django.contrib.admin'``.
|
2013-12-24 22:40:12 +08:00
|
|
|
|
|
2013-12-31 23:23:42 +08:00
|
|
|
|
This attribute defines which application the configuration applies to. It
|
|
|
|
|
must be set in all :class:`~django.apps.AppConfig` subclasses.
|
2013-12-24 22:40:12 +08:00
|
|
|
|
|
2013-12-31 23:23:42 +08:00
|
|
|
|
It must be unique across a Django project.
|
2013-12-24 22:40:12 +08:00
|
|
|
|
|
2013-12-31 23:23:42 +08:00
|
|
|
|
.. attribute:: AppConfig.label
|
2013-12-24 22:40:12 +08:00
|
|
|
|
|
2013-12-31 23:23:42 +08:00
|
|
|
|
Short name for the application, e.g. ``'admin'``
|
2013-12-24 22:40:12 +08:00
|
|
|
|
|
2014-03-01 10:03:46 +08:00
|
|
|
|
This attribute allows relabeling an application when two applications
|
2013-12-31 23:23:42 +08:00
|
|
|
|
have conflicting labels. It defaults to the last component of ``name``.
|
|
|
|
|
It should be a valid Python identifier.
|
|
|
|
|
|
|
|
|
|
It must be unique across a Django project.
|
|
|
|
|
|
|
|
|
|
.. attribute:: AppConfig.verbose_name
|
2013-12-24 22:40:12 +08:00
|
|
|
|
|
2014-03-10 04:20:34 +08:00
|
|
|
|
Human-readable name for the application, e.g. "Administration".
|
2013-12-31 23:23:42 +08:00
|
|
|
|
|
|
|
|
|
This attribute defaults to ``label.title()``.
|
2013-12-24 22:40:12 +08:00
|
|
|
|
|
2013-12-26 04:57:52 +08:00
|
|
|
|
.. attribute:: AppConfig.path
|
2013-12-24 22:40:12 +08:00
|
|
|
|
|
|
|
|
|
Filesystem path to the application directory, e.g.
|
|
|
|
|
``'/usr/lib/python2.7/dist-packages/django/contrib/admin'``.
|
|
|
|
|
|
2014-01-26 10:37:05 +08:00
|
|
|
|
In most cases, Django can automatically detect and set this, but you can
|
|
|
|
|
also provide an explicit override as a class attribute on your
|
|
|
|
|
:class:`~django.apps.AppConfig` subclass. In a few situations this is
|
|
|
|
|
required; for instance if the app package is a `namespace package`_ with
|
|
|
|
|
multiple paths.
|
|
|
|
|
|
|
|
|
|
Read-only attributes
|
|
|
|
|
--------------------
|
|
|
|
|
|
2013-12-27 01:40:28 +08:00
|
|
|
|
.. attribute:: AppConfig.module
|
2013-12-24 22:40:12 +08:00
|
|
|
|
|
|
|
|
|
Root module for the application, e.g. ``<module 'django.contrib.admin' from
|
|
|
|
|
'django/contrib/admin/__init__.pyc'>``.
|
|
|
|
|
|
2013-12-26 04:57:52 +08:00
|
|
|
|
.. attribute:: AppConfig.models_module
|
2013-12-24 22:40:12 +08:00
|
|
|
|
|
|
|
|
|
Module containing the models, e.g. ``<module 'django.contrib.admin.models'
|
|
|
|
|
from 'django/contrib/admin/models.pyc'>``.
|
|
|
|
|
|
|
|
|
|
It may be ``None`` if the application doesn't contain a ``models`` module.
|
2014-02-28 22:22:33 +08:00
|
|
|
|
Note that the database related signals such as
|
|
|
|
|
:data:`~django.db.models.signals.pre_migrate` and
|
|
|
|
|
:data:`~django.db.models.signals.post_migrate`
|
|
|
|
|
are only emitted for applications that have a ``models`` module.
|
2013-12-26 04:57:52 +08:00
|
|
|
|
|
2013-12-28 21:41:11 +08:00
|
|
|
|
Methods
|
|
|
|
|
-------
|
|
|
|
|
|
2013-12-30 03:26:13 +08:00
|
|
|
|
.. method:: AppConfig.get_models()
|
|
|
|
|
|
|
|
|
|
Returns an iterable of :class:`~django.db.models.Model` classes.
|
|
|
|
|
|
2013-12-28 21:41:11 +08:00
|
|
|
|
.. method:: AppConfig.get_model(model_name)
|
|
|
|
|
|
|
|
|
|
Returns the :class:`~django.db.models.Model` with the given
|
|
|
|
|
``model_name``. Raises :exc:`~exceptions.LookupError` if no such model
|
|
|
|
|
exists. ``model_name`` is case-insensitive.
|
|
|
|
|
|
2014-01-01 00:55:12 +08:00
|
|
|
|
.. method:: AppConfig.ready()
|
2013-12-30 19:49:53 +08:00
|
|
|
|
|
2014-01-01 00:55:12 +08:00
|
|
|
|
Subclasses can override this method to perform initialization tasks such
|
|
|
|
|
as registering signals. It is called as soon as the registry is fully
|
2013-12-30 19:49:53 +08:00
|
|
|
|
populated.
|
|
|
|
|
|
2014-01-11 06:06:19 +08:00
|
|
|
|
You cannot import models in modules that define application configuration
|
|
|
|
|
classes, but you can use :meth:`get_model` to access a model class by
|
|
|
|
|
name, like this::
|
|
|
|
|
|
|
|
|
|
def ready(self):
|
|
|
|
|
MyModel = self.get_model('MyModel')
|
|
|
|
|
|
2014-02-16 00:27:12 +08:00
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
|
|
Although you can access model classes as described above, avoid
|
|
|
|
|
interacting with the database in your :meth:`ready()` implementation.
|
|
|
|
|
This includes model methods that execute queries
|
|
|
|
|
(:meth:`~django.db.models.Model.save()`,
|
|
|
|
|
:meth:`~django.db.models.Model.delete()`, manager methods etc.), and
|
|
|
|
|
also raw SQL queries via ``django.db.connection``. Your
|
|
|
|
|
:meth:`ready()` method will run during startup of every management
|
|
|
|
|
command. For example, even though the test database configuration is
|
|
|
|
|
separate from the production settings, ``manage.py test`` would still
|
|
|
|
|
execute some queries against your **production** database!
|
|
|
|
|
|
2014-01-26 10:37:05 +08:00
|
|
|
|
.. _namespace package:
|
|
|
|
|
|
|
|
|
|
Namespace packages as apps (Python 3.3+)
|
|
|
|
|
----------------------------------------
|
|
|
|
|
|
|
|
|
|
Python versions 3.3 and later support Python packages without an
|
|
|
|
|
``__init__.py`` file. These packages are known as "namespace packages" and may
|
|
|
|
|
be spread across multiple directories at different locations on ``sys.path``
|
|
|
|
|
(see :pep:`420`).
|
|
|
|
|
|
|
|
|
|
Django applications require a single base filesystem path where Django
|
|
|
|
|
(depending on configuration) will search for templates, static assets,
|
|
|
|
|
etc. Thus, namespace packages may only be Django applications if one of the
|
|
|
|
|
following is true:
|
|
|
|
|
|
|
|
|
|
1. The namespace package actually has only a single location (i.e. is not
|
|
|
|
|
spread across more than one directory.)
|
|
|
|
|
|
|
|
|
|
2. The :class:`~django.apps.AppConfig` class used to configure the application
|
|
|
|
|
has a :attr:`~django.apps.AppConfig.path` class attribute, which is the
|
|
|
|
|
absolute directory path Django will use as the single base path for the
|
|
|
|
|
application.
|
|
|
|
|
|
|
|
|
|
If neither of these conditions is met, Django will raise
|
|
|
|
|
:exc:`~django.core.exceptions.ImproperlyConfigured`.
|
|
|
|
|
|
2013-12-26 04:57:52 +08:00
|
|
|
|
Application registry
|
|
|
|
|
====================
|
|
|
|
|
|
|
|
|
|
.. data:: apps
|
|
|
|
|
|
|
|
|
|
The application registry provides the following public API. Methods that
|
|
|
|
|
aren't listed below are considered private and may change without notice.
|
|
|
|
|
|
2014-02-14 09:50:27 +08:00
|
|
|
|
.. attribute:: apps.ready
|
2013-12-26 04:57:52 +08:00
|
|
|
|
|
2014-02-14 09:50:27 +08:00
|
|
|
|
Boolean attribute that is set to ``True`` when the registry is fully
|
|
|
|
|
populated.
|
2013-12-26 04:57:52 +08:00
|
|
|
|
|
2013-12-31 06:53:54 +08:00
|
|
|
|
.. method:: apps.get_app_configs()
|
2013-12-26 04:57:52 +08:00
|
|
|
|
|
|
|
|
|
Returns an iterable of :class:`~django.apps.AppConfig` instances.
|
|
|
|
|
|
2013-12-31 06:53:54 +08:00
|
|
|
|
.. method:: apps.get_app_config(app_label)
|
2013-12-26 04:57:52 +08:00
|
|
|
|
|
|
|
|
|
Returns an :class:`~django.apps.AppConfig` for the application with the
|
|
|
|
|
given ``app_label``. Raises :exc:`~exceptions.LookupError` if no such
|
|
|
|
|
application exists.
|
|
|
|
|
|
2014-01-07 05:48:41 +08:00
|
|
|
|
.. method:: apps.is_installed(app_name)
|
2013-12-26 04:57:52 +08:00
|
|
|
|
|
|
|
|
|
Checks whether an application with the given name exists in the registry.
|
2014-01-25 05:43:00 +08:00
|
|
|
|
``app_name`` is the full name of the app, e.g. ``'django.contrib.admin'``.
|
2013-12-26 04:57:52 +08:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
return ``False``, even though the app will become available later.
|
2013-12-28 21:41:11 +08:00
|
|
|
|
|
|
|
|
|
.. method:: apps.get_model(app_label, model_name)
|
|
|
|
|
|
|
|
|
|
Returns the :class:`~django.db.models.Model` with the given ``app_label``
|
2014-01-26 19:46:28 +08:00
|
|
|
|
and ``model_name``. As a shortcut, this method also accepts a single
|
|
|
|
|
argument in the form ``app_label.model_name``. ``model_name`` is case-
|
|
|
|
|
insensitive.
|
|
|
|
|
|
|
|
|
|
Raises :exc:`~exceptions.LookupError` if no such application or model
|
|
|
|
|
exists. Raises :exc:`~exceptions.ValueError` when called with a single
|
|
|
|
|
argument that doesn't contain exactly one dot.
|
2014-04-13 23:18:02 +08:00
|
|
|
|
|
|
|
|
|
.. _application-loading-process:
|
|
|
|
|
|
|
|
|
|
Application loading process
|
|
|
|
|
===========================
|
|
|
|
|
|
|
|
|
|
Django loads application configurations and models as soon as it starts. Here
|
|
|
|
|
are some common problems you may encounter:
|
|
|
|
|
|
|
|
|
|
* ``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.
|
|
|
|
|
|
|
|
|
|
For example, :func:`~django.utils.translation.ugettext()` uses the app
|
|
|
|
|
registry to look up translation catalogs in applications. To translate at
|
|
|
|
|
import time, you need :func:`~django.utils.translation.ugettext_lazy()`
|
|
|
|
|
instead. (Using :func:`~django.utils.translation.ugettext()` would be a bug,
|
|
|
|
|
because the translation would happen at import time, rather than at each
|
|
|
|
|
request depending on the active language.)
|
|
|
|
|
|
|
|
|
|
Executing database queries with the ORM at import time in models modules
|
|
|
|
|
will also trigger this exception. The ORM cannot function properly until all
|
|
|
|
|
models are available.
|
|
|
|
|
|
|
|
|
|
Another common culprit is :func:`django.contrib.auth.get_user_model()`. Use
|
|
|
|
|
the :setting:`AUTH_USER_MODEL` setting to reference the User model at import
|
|
|
|
|
time.
|
|
|
|
|
|
|
|
|
|
* ``ImportError: cannot import name ...`` This happens if the import sequence
|
|
|
|
|
ends up in a loop.
|
|
|
|
|
|
|
|
|
|
To eliminate such problems, you should minimize dependencies between your
|
|
|
|
|
models modules and do as little work as possible at import time. To avoid
|
|
|
|
|
executing code at import time, you can move it into a function and cache its
|
|
|
|
|
results. The code will be executed when you first need its results. This
|
|
|
|
|
concept is known as "lazy evaluation".
|
|
|
|
|
|
|
|
|
|
* ``django.contrib.admin`` will now automatically perform autodiscovery of
|
|
|
|
|
``admin`` modules in installed applications. To prevent it, change your
|
|
|
|
|
:setting:`INSTALLED_APPS` to contain
|
|
|
|
|
``'django.contrib.admin.apps.SimpleAdminConfig'`` instead of
|
|
|
|
|
``'django.contrib.admin'``.
|