Django 信号仅在 debug=True 时有效,DJANGO 3.2.4

2024-03-30

我一直在到处寻找,但找不到任何关于此的参考,我的 Django 模型信号仅在 debug=True 时才起作用,但如果 debug=False 则不起作用,这种情况在本地主机和生产服务器上都会发生。

我的设置如下所示:

设置.py

from pathlib import Path
import os
import environ

env = environ.Env()

environ.Env.read_env()

BASE_DIR = Path(__file__).resolve().parent.parent

#production
STATIC_ROOT = 'https://d1u356tnw52tcs.cloudfront.net/'


SECRET_KEY = env("SECRET_KEY_PROD")

DEBUG = True

ALLOWED_HOSTS = ['*']

CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = (
)


# Application definition
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'django.contrib.postgres',
    'sellrapp',
    'stock_management',
    'corsheaders',
    'drf_yasg',
    'optimized_image',
    'csvexport',
    'kronos',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'corsheaders.middleware.CorsMiddleware'
]

ROOT_URLCONF = '****.urls'


WSGI_APPLICATION = '****.wsgi.application'


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': env("NAME_PROD"),
        'USER': env("USER_PROD"),
        'PASSWORD': env("PASSWORD_PROD"),
        'HOST': env("HOST_PROD"),
        'PORT': env("PORT_PROD"),
    }
}

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Singapore'

USE_I18N = True

USE_L10N = True

USE_TZ = True


WEB_BASE_URL = 'https://****.com/'
BASE_URL_LIVE_CAMPAIGN = WEB_BASE_URL + "product/"

如果设置为 debug=False,则不会触发信号,并且不会出现任何错误。

信号.py

from django.dispatch import receiver
from django.db.models.signals import pre_save, post_save, pre_delete
from .models import ManualWithdrawals


@receiver(pre_delete, sender=ManualWithdrawals)
def manual_wd(sender, instance, **kwargs):
    order = ManualWithdrawals.objects.get(id=instance.id)
    consumer_order = order.order_id_list.all()
    sample_order = order.sample_order_id_list.all()
    total = str(instance.total_withdrawals).replace(".", "")
    total = int(total)
    if instance.order_id_list:
        for order in consumer_order:
            if instance.type == "reseller" and order.reseller_commission_status != "paid":
                order.reseller_commission_status = "ready for collection"
                order.save()
            if instance.type == "merchant" and order.merchant_commission_status != "paid":
                order.merchant_commission_status = "ready for collection"
                order.save()
        # updating sample order if any
    if instance.sample_order_id_list:
        for order in sample_order:
            if instance.type == "merchant" and order.merchant_commission_status != "paid":
                order.merchant_commission_status = "ready for collection"
                order.save()

这对我有用:

当你调用信号时传递weak=Falseconnect() or @receiver.


姜戈信号 https://docs.djangoproject.com/en/3.2/ref/signals/提及警告:

另请注意,Django 默认将信号处理程序存储为弱引用,因此如果您的处理程序是本地函数,它可能会被垃圾收集。为了防止这种情况,请在调用信号的 connect() 时传递weak=False。

因此,您的接收器函数可能会被垃圾收集。

例如,尝试使用:@receiver(pre_delete, sender=ManualWithdrawals, weak=False)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Django 信号仅在 debug=True 时有效,DJANGO 3.2.4 的相关文章

随机推荐