Django的“当前事务被中止,命令被忽略,直到事务块结束”仅在升级到1.5之后

时间:2013-05-27 10:40:09

标签: django postgresql django-debug-toolbar

升级到Django 1.5后,我有一个非常奇怪的错误。当我访问我的ClientDetailView并激活Django调试工具栏时,我收到以下错误:

Traceback:
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  187.                 response = middleware_method(request, response)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/debug_toolbar/panels/template.py" in process_response
  118.                     pformat(k(self.request))) for k in get_standard_processors()
File "/Users/mirkocrocop/workspace/upstream_backend/my_auth/context_processors.py" in extended_auth
  13.         user_groups = [g['name'] for g in tmp]
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/db/models/query.py" in _result_iter
  123.                 self._fill_cache()
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/db/models/query.py" in _fill_cache
  927.                     self._result_cache.append(next(self._iter))
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/db/models/query.py" in iterator
  1004.         for row in self.query.get_compiler(self.db).results_iter():
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in results_iter
  775.         for rows in self.execute_sql(MULTI):
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  840.         cursor.execute(sql, params)

Exception Type: InternalError at /clients/5/
Exception Value: current transaction is aborted, commands ignored until end of transaction block

...我知道有时我必须停用它才能看到真正的错误,但现在如果我停用DDT,错误就会完全消失。它不应该影响生产服务器,但它仍然很烦人。

以下是有问题的观点:

class BaseSingleClient(LoginRequiredMixin,
                       generic.detail.SingleObjectMixin):

    def get_context_data(self, **kwargs):
        '''
        Add the current client object to the context.
        This will be used in the template
        '''
        context = super(BaseSingleClient, self).get_context_data(**kwargs)
        try:
            self.client = self.kwargs['client_id']
        except KeyError, e:
            self.client = self.kwargs['pk']
        context['client_obj'] = UpstreamClientModel.objects.get(pk=
                                                                self.client)
        return context


class ClientDetailView(BaseSingleClient, generic.DetailView):
    model = UpstreamClientModel
    template_name = 'clients/client_detail.html'

错误发生时正在执行的SQL:

DEBUG (0.004) SELECT "django_session"."session_key", "django_session"."session_data", "django_session"."expire_date" FROM "django_session" WHERE ("django_session"."session_key" = '0ayyigap4sphq5lvtlfy64una45o4id6'  AND "django_session"."expire_date" > '2013-05-27 10:38:44.800431+00:00' ); args=('0ayyigap4sphq5lvtlfy64una45o4id6', u'2013-05-27 10:38:44.800431+00:00')
DEBUG (0.002) SELECT "my_auth_profile"."id", "my_auth_profile"."password", "my_auth_profile"."last_login", "my_auth_profile"."is_superuser", "my_auth_profile"."username", "my_auth_profile"."first_name", "my_auth_profile"."last_name", "my_auth_profile"."email", "my_auth_profile"."is_staff", "my_auth_profile"."is_active", "my_auth_profile"."date_joined", "my_auth_profile"."cost_hour", "my_auth_profile"."last_activity" FROM "my_auth_profile" WHERE "my_auth_profile"."id" = 3 ; args=(3,)
DEBUG (0.001) SELECT "auth_group"."name" FROM "auth_group" INNER JOIN "my_auth_profile_groups" ON ("auth_group"."id" = "my_auth_profile_groups"."group_id") WHERE "my_auth_profile_groups"."profile_id" = 3 ; args=(3,)

这是最后一个查询来自BTW的地方:

my_auth.context_processors.py

def extended_auth(request):
    '''
    @allowed_user (bool) the user is either a Core team member or an admin.
    @user_groups (string) the groups the user belongs to.
    '''
    is_allowed = False
    user_groups = None
    if not request.user.is_anonymous():
        tmp = request.user.groups.values('name')
        user_groups = [g['name'] for g in tmp]
        if 'Core' in user_groups or 'Administrator' in user_groups:
            is_allowed = True
    return {'allowed_user': is_allowed, 'user_groups': user_groups, }

1 个答案:

答案 0 :(得分:1)

current transaction is aborted, commands ignored until end of transaction block告诉您之前的语句导致了导致自动事务回滚的错误。

您需要检查错误日志以查找先前失败的语句。如果你在Django的日志中找不到它,请查看PostgreSQL的日志。