相关疑难解决方法(0)

TransactionManagementError"在使用信号时,您不能在"原子"块结束之前执行查询,但仅在单元测试期间执行

我在尝试保存Django User模型实例时遇到TransactionManagementError,并在其post_save信号中,我保存了一些将用户作为外键的模型.

在使用信号时,上下文和错误与此问题django TransactionManagementError非常相似

但是,在这种情况下,错误仅在单元测试时发生.

它在手动测试中运行良好,但单元测试失败.

有什么我想念的吗?

以下是代码段:

views.py

@csrf_exempt
def mobileRegister(request):
    if request.method == 'GET':
        response = {"error": "GET request not accepted!!"}
        return HttpResponse(json.dumps(response), content_type="application/json",status=500)
    elif request.method == 'POST':
        postdata = json.loads(request.body)
        try:
            # Get POST data which is to be used to save the user
            username = postdata.get('phone')
            password = postdata.get('password')
            email = postdata.get('email',"")
            first_name = postdata.get('first_name',"")
            last_name = postdata.get('last_name',"")
            user = User(username=username, email=email,
                        first_name=first_name, last_name=last_name)
            user._company = postdata.get('company',None)
            user._country_code = postdata.get('country_code',"+91")
            user.is_verified=True …
Run Code Online (Sandbox Code Playgroud)

python django unit-testing django-signals

172
推荐指数
4
解决办法
5万
查看次数

标签 统计

django ×1

django-signals ×1

python ×1

unit-testing ×1