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

前端 未结 8 959
天涯浪人
天涯浪人 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:50

    Original answer, for Django < 1.7:

    You can register the signals by importing signals.py in the app's __init__.py file:

    # __init__.py
    import signals
    

    This will allow to import models.py from signals.py without circular import errors.

    One problem with this approach is that it messes up the coverage results if you're using coverage.py.

    Related discussion

    Edit: For Django >= 1.7:

    Since AppConfig was introduced, the recommended way of importing signals is in its init() function. See Eric Marcos' answer for more details.

提交回复
热议问题