applications.txt 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.verbose_name
  83. Human-readable name for the application, e.g. "Admin".
  84. If this isn't provided, Django uses ``label.title()``.
  85. Read-only attributes
  86. --------------------
  87. .. attribute:: AppConfig.name
  88. Full Python path to the application, e.g. ``'django.contrib.admin'``.
  89. .. attribute:: AppConfig.label
  90. Last component of the Python path to the application, e.g. ``'admin'``.
  91. This value must be unique across a Django project.
  92. .. attribute:: AppConfig.path
  93. Filesystem path to the application directory, e.g.
  94. ``'/usr/lib/python2.7/dist-packages/django/contrib/admin'``.
  95. It may be ``None`` if the application isn't stored in a directory, for
  96. instance if it's loaded from an egg.
  97. .. attribute:: AppConfig.module
  98. Root module for the application, e.g. ``<module 'django.contrib.admin' from
  99. 'django/contrib/admin/__init__.pyc'>``.
  100. .. attribute:: AppConfig.models_module
  101. Module containing the models, e.g. ``<module 'django.contrib.admin.models'
  102. from 'django/contrib/admin/models.pyc'>``.
  103. It may be ``None`` if the application doesn't contain a ``models`` module.
  104. Methods
  105. -------
  106. .. method:: AppConfig.get_model(model_name)
  107. Returns the :class:`~django.db.models.Model` with the given
  108. ``model_name``. Raises :exc:`~exceptions.LookupError` if no such model
  109. exists. ``model_name`` is case-insensitive.
  110. Application registry
  111. ====================
  112. .. data:: apps
  113. The application registry provides the following public API. Methods that
  114. aren't listed below are considered private and may change without notice.
  115. .. method:: apps.ready()
  116. Returns ``True`` if the registry is fully populated.
  117. .. method:: apps.get_app_configs(only_with_models_module=False)
  118. Returns an iterable of :class:`~django.apps.AppConfig` instances.
  119. If only applications containing a models module are of interest, this method
  120. can be called with ``only_with_models_module=True``.
  121. .. method:: apps.get_app_config(app_label, only_with_models_module=False)
  122. Returns an :class:`~django.apps.AppConfig` for the application with the
  123. given ``app_label``. Raises :exc:`~exceptions.LookupError` if no such
  124. application exists.
  125. If only applications containing a models module are of interest, this method
  126. can be called with ``only_with_models_module=True``.
  127. .. method:: apps.has_app(app_name)
  128. Checks whether an application with the given name exists in the registry.
  129. ``app_name`` is the full name of the app, e.g. 'django.contrib.admin'.
  130. Unlike :meth:`~django.apps.apps.get_app_config`, this method can be called
  131. safely at import time. If the registry is still being populated, it may
  132. return ``False``, even though the app will become available later.
  133. .. method:: apps.get_model(app_label, model_name)
  134. Returns the :class:`~django.db.models.Model` with the given ``app_label``
  135. and ``model_name``. Raises :exc:`~exceptions.LookupError` if no such
  136. application or model exists. ``model_name`` is case-insensitive.