django – how to distinguish code is run by manage.py or but http server – Stack Overflow


I customized AppConfig in django to start some processes needed for the main application as described in the django documentation.

In the usual initialization process, the ready method is only called once by Django. But in some corner cases, particularly in tests which are fiddling with installed applications, ready might be called more than once. In that case, either write idempotent methods, or put a flag on your AppConfig classes to prevent rerunning code which should be executed exactly one time.

Two questions:

  1. in my experience the ready() method is alwasys called at least twice even if it just print an hello message. How can I add the flag pointed out in the documentation?
  2. I don’t want the processes start when I run manage.py commands (ex. to migrate). How can I manage this in the code?



Source link

Leave a Comment