get()返回了多个TenantUser

时间:2019-01-08 09:16:36

标签: python django django-rest-framework

我的views.py

@api_view(['POST'])
@permission_classes((AllowAny,))
@csrf_exempt
def transfer_tokens(request, domain):
    """
        transfer tokens
    """
    email = request.data['to_email']
    # user_id = request.data['connected']
    user = request.user
    coins_to_be_transferred = request.data["coins"]
    to_user = User.objects.get(email=email)
    user_id = to_user.id
    to_tuser = TenantUser.global_manager.get(user=user_id)
    tenantuser = TenantUser.global_manager.get(email=user.email)
    if to_user and user.kyc_done and to_user.kyc_done and coins_to_be_transferred >= 0:
        if user.coins >= coins_to_be_transferred:
            PoolHistory._default_manager.create(
                tenant=tenantuser, coins=coins_to_be_transferred, loss=True)
            user.coins = get_coins(tenantuser)
            user.save()
            PoolHistory._default_manager.create(
                tenant=to_tuser, coins=coins_to_be_transferred, profit=True)
            message = "You have received %s tokens from %s" % (coins_to_be_transferred, tenantuser.name)
            notification = Notifications(user=to_user, message=message)
            notification.save()
            return Response({"detail": "Tokens have been transferred successfully.", "coins": user.coins}, status=200)
        else:
            return Response({"detail": "Your account has insufficient tokens to complete the transfer."}, status=400)
    else:
        if not coins_to_be_transferred >= 0:
            return Response({"detail": "Tokens can't be transferred."}, status=400)
        if not to_user.kyc_done:
            return Response({"detail": "User with email isn't verified."}, status=400)
        else:
            return Response({"detail": "email doesn't exist."}, status=400)

1 个答案:

答案 0 :(得分:1)

由于您有多个用户使用同一封电子邮件,因此您不能为此使用get()

解决方法:

  1. 更改模型,并在unique=True字段中添加email。首先删除重复的电子邮件。

  1. 将此行to_user = User.objects.get(email=email)更改为to_user = User.objects.filter(email=email).first()