1

To keep signals organised:

__init__.py

default_app_config = 'posts.apps.PostsConfig'

apps.py

from django.apps import AppConfig


class PostsConfig(AppConfig):
    name = 'posts'

    def ready(self):
        import posts.signals

How do I ensure that ready() or import posts.signals only runs once?

StringsOnFire
  • 2,726
  • 5
  • 28
  • 50
  • 1
    Possible duplicate of [Perform model operations (only once) at server init](http://stackoverflow.com/questions/38341793/perform-model-operations-only-once-at-server-init) – solarissmoke Jul 17 '16 at 12:16
  • What problems are you encountering when `ready()` runs twice? Importing the signals twice should not cause any problems. – knbk Jul 17 '16 at 12:35
  • @solarissmoke your edit to the answer on there is what I'm looking for, thanks. The context of the question is different - that's a request for help fixing an error, this is a best-practice question based on the docs. Not fussed if it's closed, but I couldn't find your answer on that other question before I posted this. – StringsOnFire Jul 17 '16 at 12:43
  • @knbk No issue yet, just wanted to know how to account for it as the documentation says I should without an example. – StringsOnFire Jul 17 '16 at 12:43

1 Answers1

0
from django.apps import AppConfig


class PostsConfig(AppConfig):
    name = 'posts'
    ready_has_run = False

    def ready(self):
        if self.ready_has_run:
            return
        import posts.signals
        self.ready_has_run = True
StringsOnFire
  • 2,726
  • 5
  • 28
  • 50