applications.txt 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. ============
  2. Applications
  3. ============
  4. .. module:: django.apps
  5. .. versionadded:: 1.7
  6. Django contains a registry of installed applications that stores configuration
  7. and provides introspection. It also maintains a list of available :doc:`models
  8. </topics/db/models>`.
  9. This registry is simply called :attr:`~django.apps.apps` and it's available in
  10. :mod:`django.apps`::
  11. >>> from django.apps import apps
  12. >>> apps.get_app_config('admin').verbose_name
  13. 'Admin'
  14. Projects and applications
  15. =========================
  16. Django has historically used the term **project** to describe an installation
  17. of Django. A project is defined primarily by a settings module.
  18. The term **application** describes a Python package that provides some set of
  19. features. Applications may be reused in various projects.
  20. .. note::
  21. This terminology is somewhat confusing these days as it became common to
  22. use the phrase "web app" to describe what equates to a Django project.
  23. Applications include some combination of models, views, templates, template
  24. tags, static files, URLs, middleware, etc. They're generally wired into
  25. projects with the :setting:`INSTALLED_APPS` setting and optionally with other
  26. mechanisms such as URLconfs, the :setting:`MIDDLEWARE_CLASSES` setting, or
  27. template inheritance.
  28. It is important to understand that a Django application is just a set of code
  29. that interacts with various parts of the framework. There's no such thing as
  30. an ``Application`` object. However, there's a few places where Django needs to
  31. interact with installed applications, mainly for configuration and also for
  32. introspection. That's why the application registry maintains metadata in an
  33. :class:`~django.apps.AppConfig` instance for each installed application.
  34. Configuring applications
  35. ========================
  36. To configure an application, subclass :class:`~django.apps.AppConfig` and put
  37. the dotted path to that subclass in :setting:`INSTALLED_APPS`.
  38. Django uses the default :class:`~django.apps.AppConfig` class when
  39. :setting:`INSTALLED_APPS` simply contains the dotted path to an application
  40. module.
  41. For application authors
  42. -----------------------
  43. If you're creating a pluggable app called "Rock ’n’ roll", here's how you
  44. would provide a proper name for the admin::
  45. # rock_n_roll/app.py
  46. from django.apps import AppConfig
  47. class RockNRollConfig(AppConfig):
  48. name = 'rock_n_roll'
  49. verbose_name = "Rock ’n’ roll"
  50. You would then tell your users to add ``'rock_n_roll.app.RockNRollConfig'`` to
  51. their :setting:`INSTALLED_APPS`.
  52. The recommended convention is to put the configuration class in a submodule of
  53. the application called ``app``. However, this isn't enforced by Django.
  54. You must include the :attr:`~django.apps.AppConfig.name` attribute for Django
  55. to determine which application this configuration applies to. You can define
  56. any attributes documented in the :class:`~django.apps.AppConfig` API
  57. reference.
  58. For application users
  59. ---------------------
  60. If you're using "Rock ’n’ roll" in a project called ``anthology``, but you
  61. want it to show up as "Gypsy jazz" instead, you can provide your own
  62. configuration::
  63. # anthology/apps.py
  64. from rock_n_roll.app import RockNRollConfig
  65. class GypsyJazzConfig(RockNRollConfig):
  66. verbose_name = "Gypsy jazz"
  67. # anthology/settings.py
  68. INSTALLED_APPS = [
  69. 'anthology.apps.GypsyJazzConfig',
  70. # ...
  71. ]
  72. Again, defining project-specific configuration classes in a submodule called
  73. ``apps`` is a convention, not a requirement.
  74. Application configuration
  75. =========================
  76. .. class:: AppConfig
  77. Application configuration objects store metadata for an application. Some
  78. attributes can be configured in :class:`~django.apps.AppConfig`
  79. subclasses. Others are set by Django and read-only.
  80. Configurable attributes
  81. -----------------------
  82. .. attribute:: AppConfig.name
  83. Full Python path to the application, e.g. ``'django.contrib.admin'``.
  84. This attribute defines which application the configuration applies to. It
  85. must be set in all :class:`~django.apps.AppConfig` subclasses.
  86. It must be unique across a Django project.
  87. .. attribute:: AppConfig.label
  88. Short name for the application, e.g. ``'admin'``
  89. This attribute allows relabelling an application when two applications
  90. have conflicting labels. It defaults to the last component of ``name``.
  91. It should be a valid Python identifier.
  92. It must be unique across a Django project.
  93. .. attribute:: AppConfig.verbose_name
  94. Human-readable name for the application, e.g. "Admin".
  95. This attribute defaults to ``label.title()``.
  96. Read-only attributes
  97. --------------------
  98. .. attribute:: AppConfig.path
  99. Filesystem path to the application directory, e.g.
  100. ``'/usr/lib/python2.7/dist-packages/django/contrib/admin'``.
  101. It may be ``None`` if the application isn't stored in a directory, for
  102. instance if it's loaded from an egg.
  103. .. attribute:: AppConfig.module
  104. Root module for the application, e.g. ``<module 'django.contrib.admin' from
  105. 'django/contrib/admin/__init__.pyc'>``.
  106. .. attribute:: AppConfig.models_module
  107. Module containing the models, e.g. ``<module 'django.contrib.admin.models'
  108. from 'django/contrib/admin/models.pyc'>``.
  109. It may be ``None`` if the application doesn't contain a ``models`` module.
  110. Methods
  111. -------
  112. .. method:: AppConfig.get_models()
  113. Returns an iterable of :class:`~django.db.models.Model` classes.
  114. .. method:: AppConfig.get_model(model_name)
  115. Returns the :class:`~django.db.models.Model` with the given
  116. ``model_name``. Raises :exc:`~exceptions.LookupError` if no such model
  117. exists. ``model_name`` is case-insensitive.
  118. .. method:: AppConfig.ready()
  119. Subclasses can override this method to perform initialization tasks such
  120. as registering signals. It is called as soon as the registry is fully
  121. populated.
  122. Application registry
  123. ====================
  124. .. data:: apps
  125. The application registry provides the following public API. Methods that
  126. aren't listed below are considered private and may change without notice.
  127. .. method:: apps.ready()
  128. Returns ``True`` if the registry is fully populated.
  129. .. method:: apps.get_app_configs()
  130. Returns an iterable of :class:`~django.apps.AppConfig` instances.
  131. .. method:: apps.get_app_config(app_label)
  132. Returns an :class:`~django.apps.AppConfig` for the application with the
  133. given ``app_label``. Raises :exc:`~exceptions.LookupError` if no such
  134. application exists.
  135. .. method:: apps.has_app(app_name)
  136. Checks whether an application with the given name exists in the registry.
  137. ``app_name`` is the full name of the app, e.g. 'django.contrib.admin'.
  138. Unlike :meth:`~django.apps.apps.get_app_config`, this method can be called
  139. safely at import time. If the registry is still being populated, it may
  140. return ``False``, even though the app will become available later.
  141. .. method:: apps.get_model(app_label, model_name)
  142. Returns the :class:`~django.db.models.Model` with the given ``app_label``
  143. and ``model_name``. Raises :exc:`~exceptions.LookupError` if no such
  144. application or model exists. ``model_name`` is case-insensitive.