Django: INSTALLED_APPS is “.apps.AppConfig” redundant?

烈酒焚心 提交于 2021-02-11 18:14:36

问题


I didn't see an answered version of this question, and this has really been bothering me because I've seen both be used.

In this example "myapp" is the created app.

I keep seeing users setup their apps inside of the INSTALLED_APPS list like this:

INSTALLED_APPS = [
    'myapp',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

instead of

INSTALLED_APPS = [
    'myapp.apps.MyappConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

So I have 2 questions

  1. Is adding the "apps.MyappConfig" after "myapp" redundant?
  2. If there is some importance to it, what is it?

回答1:


  1. Is adding the "apps.MyappConfig" after "myapp" redundant?

Often yes, but not always. If you do not specify the AppConfig in the INSTALLED_APP yourself, then Django will look for the default_app_config variable in the module, as is specified in the documentation:

When INSTALLED_APPS contains the dotted path to an application module, Django checks for a default_app_config variable in that module.

For example in the django.contrib.sessions module [GitHub], we see:

default_app_config = 'django.contrib.sessions.apps.SessionsConfig'

It is however possible to define multiple AppConfigs in a module, that each slighly alter the behavior of the application. See next section:

  1. If there is some importance to it, what is it?

An AppConfig [Django-doc] is a class in your application that contains meta-data about the application. It contains for example the name of the app, the label, the path, et. Furthermore you can override methods like .ready() [Django-doc]. This method will be called if the models are loaded, and is often used to do some administrative tasks, or load signals when Django starts.

We can make multiple AppConfigs, for example one for development, one for production, and one for testing. But that does not happen that often.




回答2:


From Django's docs:

To configure an application, subclass AppConfig and put the dotted path to that subclass in INSTALLED_APPS.

and

If there is no default_app_config, Django uses the base AppConfig class

If you need to add extra app configurations (e.g. importing Signals), you can do so in your app config and would then need to include it in your INSTALLED_APPS. Otherwise, you can include only your app in the INSTALLED_APPS

So to answer your questions:

Is adding the "apps.MyappConfig" after "myapp" redundant?

No

If there is some importance to it, what is it?

Yes, adding extra config



来源:https://stackoverflow.com/questions/59235753/django-installed-apps-is-apps-appconfig-redundant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!