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

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

    In old Django versions would be fine to put the signals on the __init__.py or maybe in the models.py(although at the end models will be way to large for my taste).

    With Django 1.9, it is better I think, to place the signals on a signals.py file and import them with the apps.py, where they are going to be loaded after loading the model.

    apps.py:

    from django.apps import AppConfig
    
    
    class PollsConfig(AppConfig):
        name = 'polls'
    
        def ready(self):
            from . import signals  # NOQA
    

    You can also divide your signals on signals.py and handlers.py in another folder within your model named signals as well, but for me that is just over engineering. Take a look at Placing Signals

提交回复
热议问题