检查perms时,Django事务管理块以挂起的COMMIT / ROLLBACK结束

时间:2012-02-13 11:11:54

标签: python django

每当我尝试在相应视图中有Transaction managed block ended with pending COMMIT/ROLLBACK装饰器的模板中检查权限时,我都会收到此错误@transaction.commit_manually

模板:

<!-- html stuff -->
{% if perms.myApp.add_table1 %}
    {# show html elements #}
{% endif %}

删除权限条件后,不会出现任何错误。与权限无关的if条件是可以的,例如{% if user.is_superuser %}{% endif %}

UPDATE :即使权限检查不在视图呈现的模板上,但是从中扩展,它仍然存在此错误。

e.g。 nav_bar.html中的权限检查,以及扩展nav_bar.html的视图渲染费用会导致相同的错误。

view.py:

@transaction.commit_manually
def add_expense(request):
    # do stuff here

我已经尝试删除装饰器,没有任何异常,一切正常。但是当我打开装饰器时,会发生错误

urls.py:

# other stuff omitted
(r'^myApp/expenses/add/$', add_expense),

更新#2:

当用户是超级用户时,也没有问题。我相信这是因为它不会检查权限,因为用户是超级用户

我也包含了代码

@login_required()
@transaction.commit_manually
def add_expense(request):

    request.session.set_expiry(1800)

    if request.method == 'POST':
        form_input = AddExpense(request.POST)

        if form_input.is_valid():
            try:
            # after validation data is cleaned
                cd = form_input.cleaned_data
                # cleaned data is a dictionary
                input_date = date.today()
                user = request.user.username


                new_record = table1.objects.create(
                    amount = cd['amount'],
                    date = cd['date_of_expense'],
                    username = user
                    )
                new_record.save()
                transaction.commit()
                return render_to_response('forms/add_expense_success.html', context_instance=RequestContext(request))
            except Exception, e:
                pass
                transaction.rollback()
                return HttpResponse(None)
        else:

            return render_to_response('forms/add_expense.html', {'form': form_input},
                              context_instance=RequestContext(request))
    else:
        # loading this gives error, not sure the top part
        form = AddExpense()
        return render_to_response('forms/add_expense.html', {'form': form, 'page_title': '新增支出'},
                      context_instance=RequestContext(request))

3 个答案:

答案 0 :(得分:0)

只是为了确认错误信息的含义,你在Postgres上,对吗?

如果是这样,有一个建议:在上面的# do stuff here区块中,我敢打赌,即使您在写作之后,也不会在读取后手动提交。

从Django 1.3开始,事务管理的读取被视为脏 - 不仅仅是写入 - 所以在这之后你还需要commit()

有关详细信息,请参阅release notes

答案 1 :(得分:0)

由context_instance = RequestContext(request),

引起

通过@Marcin

查看此处的解决方案

Django Transaction managed block ended with pending COMMIT/ROLLBACK

答案 2 :(得分:0)

我在尝试减少访问用户的权限时收到此错误消息。

  • postgresql 9.1`
  • python 2.7.3
  • django 1.3.1

如果以超级用户(管理员角色)身份运行,则根本没有问题,如果以用户(用户角色)身份运行,则会出现错误。 用户对所有django相关表甚至数据库和模式都拥有所有(甚至授予)权限,但它仍然会出错。

是什么原因引起的?到目前为止,我可以调试它,它只是缺少超级用户权限,否则超级用户和用户拥有相同的权限。

代码:

@login_required
@transaction.commit_manually
def sipuser_add(request, extension_id):
    member = members.objects.get(nickname=request.user.username)
    extension = extensions.objects.get(id=extension_id)
    nickname = request.user.username
    if extension.id_members.nickname == member.nickname:
        from django.db import connection
        cursor = connection.cursor()
        secret = pwgen()
        cursor.execute("SELECT func_create_sipuser('%s','%s',%s)" % (nickname, secret, extension_id) )
        ret = cursor.fetchone()
        logger.debug("created extension_id: %s - stored procedure 'func_create_sipuser' returned: %s" % (extension_id, ret) )
        pin = 1234
        timeout = 15
        cursor.execute("SELECT func_create_voicemail_from_phonenumberid(%s,'%s',%s)" % (extension_id, pin, timeout) )
        ret = cursor.fetchone()
        logger.debug("created voicemail for extension_id %s - stored procedure 'func_create_voicemail' returned: %s" % (extension_id, ret) )
        cursor.close() # is this needed and/or on the right position called?
        connection.commit()
    return HttpResponseRedirect('/extensions/')