How does it work, the naming convention for Django INSTALLED_APPS?
Question:
The tutorial on the site creates an app named polls. It’s using django 1.9, so in the INSTALLED_APPS it is:
polls.apps.PollsConfig
I’m watching a tutorial he names the app newsletter and in INSTALLED_APPS he has
newsletter
he’s using 1.8, though. I am using 1.9. I’ve watched other tutorials and they also just add a name without dots in the syntax as he does. I realize things may be different, that’s understood. To be clear if I named my app dogs,. in the installed apps it would be named like this
dogs.apps.DogsConfig
or if it was tree it would be
tree.apps.TreeConfig
Is that how the naming convention goes? also I would assume things would get shorter in newer versions and more convenient. so to go from just adding
newsletter,
to having to type out
polls.apps.PollsConfig
seems weird to me. But I’m new so I maybe missing something. Any and all advice is welcome
Answers:
That is the Application Configuration feature, new to Django 1.7.
Basically, now you can list in INSTALLED_APPS
either the module that contains the application or a class that derives from django.apps.AppConfig
and defines the behavior of the application.
This feature provides several advantages:
- Apps can be configured more easily, and even subclassed for customization.
- You can have several apps in the same module.
Application modules can define the special module variable default_app_config
to specify the name of their AppConfig
, so that they can use the new features without having to specify the full name of that class in INSTALLED_APPS
. But this is a backwards compatibility feature and new applications are recommended to write the full AppConfig
name.
Anyway, most django/contrib
apps use that default_app_config
, for compatibility with old configurations. See for example the file django/contrib/messages/__init__.py
is just:
from django.contrib.messages.api import *
from django.contrib.messages.constants import *
default_app_config = 'django.contrib.messages.apps.MessagesConfig'
So, adding it up, per OP request:
- If you add in
INSTALLED_APPS
the typename foo.apps.FooConfig
, then that class will be used to setup the foo
app, 1.7 style (recommended).
-
If you add in INSTALLED_APPS
the plain name foo
, then:
- if there is a variable
foo.default_app_config
this class will be used to setup the foo
app, 1.7 style. Most (all?) the standard Django apps have this variable, so that you don’t need to change your INSTALLED_APPS
when you upgrade from Django-1.6 to Django-1.7.
- if there is not such a variable, then the 1.6 style application will be used, with default values for the advanced configuration options.
In the setup.py ,under the Installed apps just add app_name
like
INSTALLED_APPS = [
'polls', # <--- here
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
While I was searching about INSTALLED_APPS constant like you I have seen this explanation on documents:
A list of strings designating all applications that are enabled in this Django installation. Each string should be a dotted Python path to:
- an application configuration class (preferred), or
- a package containing an application.
The second bullet is explaining that you can use your application folder for resolving the existing settings automatically. The first option can be also used for activate your application. But the most important thing is that it is preferred one.
If you want to go with the preferred one first you have to create app.py in your new application folder then set like this:
# my_new_app/apps.py
from django.apps import AppConfig
class MyNewAppConfig(AppConfig):
name = 'my_new_app'
verbose_name = "My Brand New Application"
# my_new_app/__init__.py
default_app_config = 'my_new_app.apps.MyNewAppConfig'
Why the first one is preferred? Because I think of that it is explicit.
The tutorial on the site creates an app named polls. It’s using django 1.9, so in the INSTALLED_APPS it is:
polls.apps.PollsConfig
I’m watching a tutorial he names the app newsletter and in INSTALLED_APPS he has
newsletter
he’s using 1.8, though. I am using 1.9. I’ve watched other tutorials and they also just add a name without dots in the syntax as he does. I realize things may be different, that’s understood. To be clear if I named my app dogs,. in the installed apps it would be named like this
dogs.apps.DogsConfig
or if it was tree it would be
tree.apps.TreeConfig
Is that how the naming convention goes? also I would assume things would get shorter in newer versions and more convenient. so to go from just adding
newsletter,
to having to type out
polls.apps.PollsConfig
seems weird to me. But I’m new so I maybe missing something. Any and all advice is welcome
That is the Application Configuration feature, new to Django 1.7.
Basically, now you can list in INSTALLED_APPS
either the module that contains the application or a class that derives from django.apps.AppConfig
and defines the behavior of the application.
This feature provides several advantages:
- Apps can be configured more easily, and even subclassed for customization.
- You can have several apps in the same module.
Application modules can define the special module variable default_app_config
to specify the name of their AppConfig
, so that they can use the new features without having to specify the full name of that class in INSTALLED_APPS
. But this is a backwards compatibility feature and new applications are recommended to write the full AppConfig
name.
Anyway, most django/contrib
apps use that default_app_config
, for compatibility with old configurations. See for example the file django/contrib/messages/__init__.py
is just:
from django.contrib.messages.api import *
from django.contrib.messages.constants import *
default_app_config = 'django.contrib.messages.apps.MessagesConfig'
So, adding it up, per OP request:
- If you add in
INSTALLED_APPS
the typenamefoo.apps.FooConfig
, then that class will be used to setup thefoo
app, 1.7 style (recommended). -
If you add in
INSTALLED_APPS
the plain namefoo
, then:- if there is a variable
foo.default_app_config
this class will be used to setup thefoo
app, 1.7 style. Most (all?) the standard Django apps have this variable, so that you don’t need to change yourINSTALLED_APPS
when you upgrade from Django-1.6 to Django-1.7. - if there is not such a variable, then the 1.6 style application will be used, with default values for the advanced configuration options.
- if there is a variable
In the setup.py ,under the Installed apps just add app_name
like
INSTALLED_APPS = [
'polls', # <--- here
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
While I was searching about INSTALLED_APPS constant like you I have seen this explanation on documents:
A list of strings designating all applications that are enabled in this Django installation. Each string should be a dotted Python path to:
- an application configuration class (preferred), or
- a package containing an application.
The second bullet is explaining that you can use your application folder for resolving the existing settings automatically. The first option can be also used for activate your application. But the most important thing is that it is preferred one.
If you want to go with the preferred one first you have to create app.py in your new application folder then set like this:
# my_new_app/apps.py
from django.apps import AppConfig
class MyNewAppConfig(AppConfig):
name = 'my_new_app'
verbose_name = "My Brand New Application"
# my_new_app/__init__.py
default_app_config = 'my_new_app.apps.MyNewAppConfig'
Why the first one is preferred? Because I think of that it is explicit.