问题
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
- Is adding the "apps.MyappConfig" after "myapp" redundant?
- If there is some importance to it, what is it?
回答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 adefault_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 AppConfig
s in a module, that each slighly alter the behavior of the application. See next section:
- 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 AppConfig
s, 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