无法使用Django中间件autocommit

2024-04-25 12:08:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我们的代码有定制的中间件来记录请求和响应,所以我们可以记录API活动。你知道吗

def process_response(self, request, response):
    user = None
    try:
        if request.user.is_authenticated():
            user = request.user
        the_record = Record(
            requestUser=user,
            requestPath=request.path,
            requestMethod=request.method)
        the_record.save()
    except:
        pass

在响应方法的过程中,我们创建对象并保存它。如果API方法标记为@transaction.atomic,则会出现日志消息autocommit cannot be used inside a transaction。你知道吗

方法中没有异常,但只在数据库中记录,在这种情况下,该特定API调用不存在。你知道吗

有什么解释为什么会出现自动提交日志消息吗?也许使用模型管理器create而不是save方法?你知道吗


Tags: 中间件the方法代码api消息responserequest