私を狂わせて助けてください....
管理者で新しいプロジェクトを保存すると、同じものが3回保存されます。誰かが理由を見つけることができますか?これは、Django1.5を実行してインストールされたDjangoデバッグツールバーでのみ発生します
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)