I noticed different ways to register apps in django project. For example, if I have an app called Catalog, in settings.py I can use either just the name of the app 'catalog' or the longer name 'catalog.apps.CatalogConfig'. What is the difference and which one is more accurate way to register an app?
You (app user) should only care about app's name:
INSTALLED_APPS = (
...
'rock_n_roll',
...
)
This is because app author subclasses AppConfig
to configure and provide proper name for their app:
# rock_n_roll/apps.py
from django.apps import AppConfig
class RockNRollConfig(AppConfig):
name = 'rock_n_roll'
verbose_name = "Rock ’n’ roll"
From documentation:
That will cause
RockNRollConfig
to be used whenINSTALLED_APPS
just containsrock_n_roll
. This allows you to make use ofAppConfig
features without requiring your users to update theirINSTALLED_APPS
setting. Besides this use case, it’s best to avoid usingdefault_app_config
and instead specify the app config class inINSTALLED_APPS
as described next.
Why use AppConfig
at all?
It is important to understand that a Django application is just a set of code that interacts with various parts of the framework. There’s no such thing as an Application object. However, there’s a few places where Django needs to interact with installed applications, mainly for configuration and also for introspection. That’s why the application registry maintains metadata in an
AppConfig
instance for each installed application.
Let suppose you generated the app named todos the it should be written as:-
INSTALLED_APPS = [
...
'todos.apps.TodosConfig',
...
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With