development

Django 프로젝트에서 내 signal.py 파일을 보관할 올바른 위치

big-blog 2020. 10. 15. 08:01
반응형

Django 프로젝트에서 내 signal.py 파일을 보관할 올바른 위치


내가 읽은 Django의 문서 signals.py에 따르면 앱 폴더에서 시작하기에 좋은 곳인 것 같지만 내가 직면 한 문제는 신호를 생성 pre_save하고 모델에서 클래스를 가져 오려고 할 때 import내 모델에서.

# models.py

from django.contrib.auth.models import User
from django.db import models
from django.utils.translation import gettext as _
from signals import *

class Comm_Queue(CommunicatorAbstract):
    queue_statuses = (
        ('P', _('Pending')),
        ('S', _('Sent')),
        ('E', _('Error')),
        ('R', _('Rejected')),
    )
    status          = models.CharField(max_length=10, db_index=True, default='P')
    is_html         = models.BooleanField(default=False)
    language        = models.CharField(max_length=6, choices=settings.LANGUAGES)
    sender_email    = models.EmailField()
    recipient_email = models.EmailField()
    subject         = models.CharField(max_length=100)
    content         = models.TextField()

# signals.py

from django.conf import settings
from django.db.models.signals import pre_save
from django.dispatch import receiver
from models import Comm_Queue

@receiver(pre_save, sender=Comm_Queue)
def get_sender_email_from_settings(sender, **kwargs):
    obj=kwargs['instance']
    if not obj.sender_email:
        obj.sender_email='%s' % settings.ADMINS[0][1]

이 코드는 Comm_Queue내부를 가져오고 내부 signals.py신호도 가져 오기 때문에 실행되지 않습니다 models.py.

이 문제를 극복하는 방법에 대한 조언을받을 수 있습니까?

문안 인사


Django <1.7에 대한 원래 답변 :

signals.py앱의 __init__.py파일 에서 가져 와서 신호를 등록 할 수 있습니다 .

# __init__.py
import signals

이렇게하면 순환 가져 오기 오류없이 models.py에서 signals.py가져올 수 있습니다.

이 접근 방식의 한 가지 문제점은 coverage.py를 사용하는 경우 적용 범위 결과가 엉망이라는 것입니다.

관련 토론

편집 : Django> = 1.7 :

AppConfig가 도입 된 이후로 권장되는 신호 가져 오기 방법은 init()기능입니다. 자세한 내용은 Eric Marcos의 답변 을 참조하십시오.


Django <= 1.6을 사용하는 경우 Kamagatos 솔루션을 권장합니다. 모델 모듈의 끝에 신호를 가져옵니다.

Django의 향후 버전 (> = 1.7)의 경우 권장되는 방법은 앱의 config ready () 함수 에서 신호 모듈을 가져 오는 것입니다 .

my_app/apps.py

from django.apps import AppConfig

class MyAppConfig(AppConfig):
    name = 'my_app'

    def ready(self):
        import my_app.signals

my_app/__init__.py

default_app_config = 'my_app.apps.MyAppConfig'

문제를 해결하려면 모델 정의 후에 신호를 가져 오기만하면됩니다. 그게 다야.


또한 signal.py 파일에 신호를 넣고 모든 신호를로드하는이 코드 스 니펫도 있습니다.

# import this in url.py file !

import logging

from importlib import import_module

from django.conf import settings

logger = logging.getLogger(__name__)

signal_modules = {}

for app in settings.INSTALLED_APPS:
    signals_module = '%s.signals' % app
    try:
        logger.debug('loading "%s" ..' % signals_module)
        signal_modules[app] = import_module(signals_module)
    except ImportError as e:
        logger.warning(
            'failed to import "%s", reason: %s' % (signals_module, str(e)))

This is for project, I'm not sure if it works at app level.


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


I'm guessing that you're doing that so your signals are registered, so that they're found somewhere. I just put my signals right in a models.py file normally.


An alternative is to import the callback functions from signals.py and connect them in models.py:

signals.py

def pre_save_callback_function(sender, instance, **kwargs):
    # Do stuff here

model.py

# Your imports here
from django.db.models.signals import pre_save
from yourapp.signals import pre_save_callback_function

class YourModel:
    # Model stuff here
pre_save.connect(pre_save_callback_function, sender=YourModel)

Ps: Importing YourModel in signals.py will create a recursion; use sender, instead.

Ps2: Saving the instance again in the callback function will create a recursion. You can make a control argument in .save method to control it.


This only applies if you have your signals in a separate signals.py file

In completely agree with the answer of @EricMarcos but it should be stated that the django docs explicitly advice not to use the default_app_config variable (although it is not wrong). For current versions, correct way would be:

my_app/apps.py

from django.apps import AppConfig

class MyAppConfig(AppConfig):
    name = 'my_app'

    def ready(self):
        import my_app.signals

settings.py

(Make sure you don't just have your app name in installed apps but instead the relative path to your AppConfig)

INSTALLED_APPS = [
    'my_app.apps.MyAppConfig',
    # ...
]

참고URL : https://stackoverflow.com/questions/7115097/the-right-place-to-keep-my-signals-py-file-in-a-django-project

반응형