django constance导致事务错误

时间:2018-11-01 13:24:19

标签: django postgresql

我使用django 1.11,管理员可以配置密码是否应该很复杂,因此我使用constace来启用此配置。

settings.py

AUTH_PASSWORD_VALIDATORS = [
{
    'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
    'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    'OPTIONS': {
        'min_length': 8,
    }
},
{
    'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
     'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
{'NAME': 'apps.core.password_validators.NumberValidator', },
# {'NAME': '.apps.core.password_validators.UppercaseValidator', },
# {'NAME': '.apps.core.password_validators.LowercaseValidator', },
# {'NAME': '.apps.core.password_validators.SymbolValidator', }
]

CONSTANCE_CONFIG = {
'PASSWORD_LIFETIME': (365*25, _('The time in Days how long a Password is valid'), int),
'PASSWORD_COMPLEXITY': (True, _('When set to true the Password must contain symbols, numbers, and upper/lowercase letters.')),
'SESSION_COOKIE_AGE': (14*24*60, _('The time in minutes how long a session is valid'), int),
}

password_validation.py:     从constance导入配置

class NumberValidator(object):
def validate(self, password, user=None):
    is_complex = bool(config.PASSWORD_COMPLEXITY)

    if is_complex:
        if not re.findall('\d', password):
            raise ValidationError(
                _("The password must contain at least 1 digit, 0-9."),
                code='password_no_number',
            )

def get_help_text(self):
    if config.PASSWORD_COMPLEXITY:
        return _(
            "Your password must contain at least 1 digit, 0-9."
        )
    else:
        return ''

通过管理界面创建新用户时,我得到以下信息:

当前事务中止,命令被忽略,直到事务块结束

当我删除config.PASSWORD_COMPLEXITY部分时,代码起作用。

有人知道为什么使用常数会导致这种类型的错误吗?

完整追溯:

Request Method: POST
Request URL: http://localhost:8001/myappuseradmin/core/user/add/

Django Version: 1.11.13
Python Version: 2.7.9
Installed Applications:
('my_app.apps.core.apps.CoreConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.postgres',
 'django_extensions',
 'djangojames',
 'django_filters',
 'rest_framework',
 'rosetta',
 'stronghold',
 'channels',
 'django_celery_results',
 'health_check',
 'health_check.db',
 'health_check.cache',
 'health_check.storage',
 'health_check.contrib.celery',
 'constance',
 'my_app.apps.bokeh_server',
 'my_app.apps.events',
 'my_app.apps.analysis',
 'my_app.apps.timeseries',
 'my_app.apps.cases',
 'my_app.apps.pec_controller',
 'my_app.apps.system',
 'my_app.apps.measurements',
 'my_app.apps.landing_page',
 'my_app.apps.opc_da_client',
 'my_app.apps.opc_ae_client',
 'my_app.apps.importexport')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'stronghold.middleware.LoginRequiredMiddleware',
 'my_app.apps.core.middleware.security_request_logging_middleware',
 'my_app.apps.core.middleware.password_expired_middleware',
 'my_app.apps.core.middleware.session_expiry_middleware')



Traceback:

File "/my_app-env/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/my_app-env/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/my_app-env/lib/python2.7/site-packages/channels/handler.py" in process_exception_by_middleware
  243.             return super(AsgiHandler, self).process_exception_by_middleware(exception, request)

File "/my_app-env/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/my_app-env/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
  551.                 return self.admin_site.admin_view(view)(*args, **kwargs)

File "/my_app-env/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/my_app-env/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)

File "/my_app-env/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
  224.             return view(request, *args, **kwargs)

File "/my_app-env/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
  67.             return bound_func(*args, **kwargs)

File "/my_app-env/lib/python2.7/site-packages/django/views/decorators/debug.py" in sensitive_post_parameters_wrapper
  76.             return view(request, *args, **kwargs)

File "/my_app-env/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
  63.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "/my_app-env/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
  67.             return bound_func(*args, **kwargs)

File "/my_app-env/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/my_app-env/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
  63.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "/my_app-env/lib/python2.7/site-packages/django/contrib/auth/admin.py" in add_view
  103.             return self._add_view(request, form_url, extra_context)

File "/my_app-env/lib/python2.7/site-packages/django/contrib/auth/admin.py" in _add_view
  131.                                                extra_context)

File "/my_app-env/lib/python2.7/site-packages/django/contrib/admin/options.py" in add_view
  1508.         return self.changeform_view(request, None, form_url, extra_context)

File "/my_app-env/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
  67.             return bound_func(*args, **kwargs)

File "/my_app-env/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/my_app-env/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
  63.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "/my_app-env/lib/python2.7/site-packages/django/contrib/admin/options.py" in changeform_view
  1408.             return self._changeform_view(request, object_id, form_url, extra_context)

File "/my_app-env/lib/python2.7/site-packages/django/contrib/admin/options.py" in _changeform_view
  1440.             if form.is_valid():

File "/my_app-env/lib/python2.7/site-packages/django/forms/forms.py" in is_valid
  183.         return self.is_bound and not self.errors

File "/my_app-env/lib/python2.7/site-packages/django/forms/forms.py" in errors
  175.             self.full_clean()

File "/my_app-env/lib/python2.7/site-packages/django/forms/forms.py" in full_clean
  384.         self._clean_fields()

File "/my_app-env/lib/python2.7/site-packages/django/forms/forms.py" in _clean_fields
  405.                     value = getattr(self, 'clean_%s' % name)()

File "/my_app-env/lib/python2.7/site-packages/django/contrib/auth/forms.py" in clean_password2
  106.         password_validation.validate_password(self.cleaned_data.get('password2'), self.instance)

File "/my_app-env/lib/python2.7/site-packages/django/contrib/auth/password_validation.py" in validate_password
  52.             validator.validate(password, user)

File "/my_app/src/my_app/apps/core/password_validators.py" in validate
  21.         is_complex = config.PASSWORD_COMPLEXITY

File "/my_app-env/lib/python2.7/site-packages/django/utils/functional.py" in inner
  239.         return func(self._wrapped, *args)

File "/my_app-env/lib/python2.7/site-packages/constance/base.py" in __getattr__
  22.             setattr(self, key, default)

File "/my_app-env/lib/python2.7/site-packages/constance/base.py" in __setattr__
  29.         self._backend.set(key, value)

File "/my_app-env/lib/python2.7/site-packages/constance/backends/database/__init__.py" in set
  84.         old_value = self.get(key)

File "/my_app-env/lib/python2.7/site-packages/constance/backends/database/__init__.py" in get
  75.                 value = self._model._default_manager.get(key=key).value

File "/my_app-env/lib/python2.7/site-packages/django/db/models/manager.py" in manager_method
  85.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/my_app-env/lib/python2.7/site-packages/django/db/models/query.py" in get
  374.         num = len(clone)

File "/my_app-env/lib/python2.7/site-packages/django/db/models/query.py" in __len__
  232.         self._fetch_all()

File "/my_app-env/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
  1118.             self._result_cache = list(self._iterable_class(self))

File "/my_app-env/lib/python2.7/site-packages/django/db/models/query.py" in __iter__
  53.         results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)

File "/my_app-env/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  899.             raise original_exception

Exception Type: InternalError at /myappuseradmin/core/user/add/
Exception Value: current transaction is aborted, commands ignored until end of transaction block

1 个答案:

答案 0 :(得分:0)

解决问题的方法如下: 我错过了将'constance.backends.database'应用添加到已安装的django应用设置中的功能。

错误很难发现。关键是检查postgres日志,该日志显示不存在常数表。