How to override Django apps.py and its AppConfig for third-party applications that are not?

Since Django 1.7, a function has been added AppConfig, suggesting that post_migrate signals should be placed in part of ready()their custom implementation - https://docs.djangoproject.com/es/1.9/ref/signals/#post-migrate

The main implementation method AppConfigdescribed in the documents is to specify it in the file __init__.pyusing the parameter default_app_config. The docs also offer a way to override an existing one AppConfigfor any application: https://docs.djangoproject.com/es/1.9/ref/applications/#for-application-users

I studied a little and found out that django actually creates an instance AppConfigfor each application in INSTALLED_APPS, even if its custom implementation is not implemented, it will load by default for you.

My question is how to configure a custom application configuration using a signal post_migratefor an application that does not implement AppConfig(the simplest example would be a third-party package without apps.py)?

I know that even for this application django will go and create a standard version AppConfig, but where and how should I say this NOT to do this, and instead use my own AppConfiginstead of overriding the implementation of the ready()add method post_migrate?

+2
source share
3 answers

, AppConfig django crispy_forms ( virtualenv).

1. crispy_forms . 2. __init__.py

from django.apps import AppConfig

default_app_config = 'crispy_forms.apps.MyAppConfig'

3: apps.py crispy_forms

from django.apps import AppConfig

class MyAppConfig(AppConfig):

    verbose_name = 'customized crispy_forms'
    name = "crispy_forms"

    def __init__(self, app_name, app_module):
        AppConfig.__init__(self,app_name, app_module)
        print 'My app config', app_name, app_module

django , , - :

My app config crispy_forms <module 'crispy_forms' from '/home/xxx/project/crispy_forms/__init__.pyc'>
+6

, . Django "django-constance" Admin.
, , webapp.

, , @canufeel, , constance , " ".

:

"myApp/constance", , @canufeel. , constance INSTALLED_APPS, "myapp.constance", :

MYAPP_CONSTANCE_CONFIG_VAR = {
'ACCOUNT_ACTIVATION_DAYS': (7, 'Number of days before activation is kept available'),
'BOOTSTRAP_THEME': ('slate', 'Bootstrap theme for front end', 'select_theme'),
...
}

:

BASE_CONSTANCE_CONFIG = getattr(settings, 'CONSTANCE_CONFIG', {})
BASE_CONSTANCE_CONFIG.update(MYAPP_CONSTANCE_CONFIG_VAR)

settings.CONSTANCE_CONFIG = BASE_CONSTANCE_CONFIG_VAR
settings.CONSTANCE_ADDITIONAL_FIELDS = BASE_CONSTANCE_ADDITIONAL_FIELDS
settings.CONSTANCE_CONFIG_FIELDSETS = WAVES_CONSTANCE_CONFIG_FIELDSETS

, , "" : -)

0

, . . :

:

"--" , , , "Jazz Manouche", :

# anthology/apps.py

from rock_n_roll.apps import RockNRollConfig

class JazzManoucheConfig(RockNRollConfig):
    verbose_name = "Jazz Manouche"

# anthology/settings.py

INSTALLED_APPS = [
    'anthology.apps.JazzManoucheConfig',
    # ...
]
0

Source: https://habr.com/ru/post/1687685/


All Articles