Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to register apps in Django

Tags:

django

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?

like image 660
Sergei V Kim Avatar asked Jan 30 '17 10:01

Sergei V Kim


2 Answers

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 when INSTALLED_APPS just contains rock_n_roll. This allows you to make use of AppConfig features without requiring your users to update their INSTALLED_APPS setting. Besides this use case, it’s best to avoid using default_app_config and instead specify the app config class in INSTALLED_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.

like image 195
Siegmeyer Avatar answered Sep 28 '22 05:09

Siegmeyer


Let suppose you generated the app named todos the it should be written as:-

INSTALLED_APPS = [
...
'todos.apps.TodosConfig',
...
]
like image 44
Jayendra Narayan Singh Avatar answered Sep 28 '22 05:09

Jayendra Narayan Singh