The right place to keep my signals.py file in a Django project

前端 未结 8 937
天涯浪人
天涯浪人 2020-11-27 14:24

Based on Django\'s documentation I was reading, it seems like signals.py in the app folder is a good place to start with, but the problem I\'m facing is that wh

8条回答
  •  遥遥无期
    2020-11-27 14:46

    This only applies if you have your signals in a separate signals.py file

    In completely agree with the answer of @EricMarcos but it should be stated that the django docs explicitly advice not to use the default_app_config variable (although it is not wrong). For current versions, correct way would be:

    my_app/apps.py

    from django.apps import AppConfig
    
    class MyAppConfig(AppConfig):
        name = 'my_app'
    
        def ready(self):
            import my_app.signals
    

    settings.py

    (Make sure you don't just have your app name in installed apps but instead the relative path to your AppConfig)

    INSTALLED_APPS = [
        'my_app.apps.MyAppConfig',
        # ...
    ]
    

提交回复
热议问题