Django 调试工具栏问题在管理员中多次保存

标签 django django-models django-admin

快把我逼疯了请帮忙....

在管理员中,当我保存一个新项目时,同样的东西保存了 3 次!谁能看出为什么?这只发生在安装了运行 Django 1.5 的 Django 调试工具栏上

enter image description here

settings.py(部分)

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    #     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

TEMPLATE_DIRS = (
    LOCAL_PATH + '/templates/',
)

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.core.context_processors.request",
    "django.contrib.messages.context_processors.messages",

    "allauth.account.context_processors.account",
    "allauth.socialaccount.context_processors.socialaccount",
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.flatpages',
    'grappelli.dashboard',
    'grappelli',
    'django.contrib.admin',
    'django.contrib.admindocs',
    'storages',
    'south',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.facebook',
    #'allauth.socialaccount.providers.google',
    #'allauth.socialaccount.providers.twitter',
    'rest_framework',
    'rest_framework.authtoken',
    'products',

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}


# debug_toolbar settings
if DEBUG:
    INTERNAL_IPS = ('127.0.0.1',)
    MIDDLEWARE_CLASSES += (
        'debug_toolbar.middleware.DebugToolbarMiddleware',
    )

    INSTALLED_APPS += (
        'debug_toolbar',
    )

    DEBUG_TOOLBAR_PANELS = (
        'debug_toolbar.panels.version.VersionDebugPanel',
        'debug_toolbar.panels.timer.TimerDebugPanel',
        'debug_toolbar.panels.settings_vars.SettingsVarsDebugPanel',
        'debug_toolbar.panels.headers.HeaderDebugPanel',
        'debug_toolbar.panels.profiling.ProfilingDebugPanel',
        'debug_toolbar.panels.request_vars.RequestVarsDebugPanel',
        'debug_toolbar.panels.sql.SQLDebugPanel',
        'debug_toolbar.panels.template.TemplateDebugPanel',
        'debug_toolbar.panels.cache.CacheDebugPanel',
        'debug_toolbar.panels.signals.SignalDebugPanel',
        'debug_toolbar.panels.logger.LoggingPanel',
    )

    DEBUG_TOOLBAR_CONFIG = {
        'INTERCEPT_REDIRECTS': False,
        }

模型保存方法

def product_pre_save(sender, instance, **kwargs):
    """
    This is sent at the beginning of a the product save() method.
    """
    if not instance.pk:
        instance._QRCODE = True
    else:
        if hasattr(instance, '_QRCODE'):
            instance._QRCODE = False
        else:
            instance._QRCODE = True


models.signals.pre_save.connect(product_pre_save, sender=Product)


# def product_pre_delete(sender, instance, **kwargs):
#     """
#     Sent at the beginning of a product delete() method product queryset's delete() method.
#     """
#     if default_storage.exists(instance.qr_image):
#         default_storage.delete(instance.qr_image)

# models.signals.pre_delete.connect(product_pre_delete, sender=Product)

def product_post_save(sender, instance, **kwargs):
    if hasattr(instance, '_already_saving'):
        del instance._already_saving
        return
    if instance._QRCODE:
        instance._QRCODE = False
    if instance.qr_image:
        instance.qr_image.delete()
        # Create url
    instance.qr_url = instance.create_QR_URL()
    qr = QRCode(4, QRErrorCorrectLevel.L)
    qr.addData(instance.qr_url)
    qr.make()
    image = qr.makeImage()


    #Save image to string buffer
    image_buffer = StringIO()
    image.save(image_buffer, format='JPEG')
    image_buffer.seek(0)

    #Here we use django file storage system to save the image.
    file_name = 'UrlQR_%s.jpg' % instance.id
    file_object = File(image_buffer, file_name)
    content_file = ContentFile(file_object.read())
    instance._already_saving = True
    instance.qr_image.save(file_name, content_file, save=True)


models.signals.post_save.connect(product_post_save, sender=Product)

最佳答案

是 ProfilingDebugPanel 导致 Admin 模型保存多个数据。他们没有针对该错误的最新提交。到目前为止,他们一直在寻找解决方案。

更新:

状态:
终于修复了,但还没有发布。

临时解决方案:
禁用探查器可修复此错误。

关于Django 调试工具栏问题在管理员中多次保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15139860/

相关文章:

python - Pycharm 无法识别 shell 可以识别的包。 Django

django - 多个带有 nginx proxy_pass 的 django 应用程序并重写

django - 如何使用 nginx 和 docker 作为反向代理

python - 如何在 Django 模型中存储元组列表?

django - from rest_framework.filters import SearchFilter 生成错误,因为无法从 'django.db 导入名称 'ORDER_PATTERN'

django - 如何在日期字段上显示日期选择器日历

python - 在 Django 中创建时将用户添加到组

Django 模型 : Set default relative to another field

django - 如何组合两个模型并在管理部分只制作一个表格?

django - 在 Django 管理 ListView 中避免 n+1 查询