How to avoid AppConfig.ready() method running twice in Django

前端 未结 4 1689
野性不改
野性不改 2020-12-05 06:32

I want to execute some code at startup of Django server but I want it to run only once. Currently when I start the server it\'s executed twice. Documentation says that this

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 06:46

    if you don't want to use --noreload you can:

    replace the line in your app's __init__.py that you use to specify the config:

    default_app_config = 'mydjangoapp.apps.MydjangoappConfig'
    

    by this:

    import os
    
    if os.environ.get('RUN_MAIN', None) != 'true':
        default_app_config = 'mydjangoapp.apps.MydjangoappConfig'
    

提交回复
热议问题