You need to enable JavaScript to run this app. Django接口报paymentForOrderID参数缺失及重复值拦截问题求助
优惠活动
大模型
产品
解决方案
定价
更多

Django接口报paymentForOrderID参数缺失及重复值拦截问题求助

错误汇总
    1. 路由配置不匹配:视图函数addProductPaymentResponse声明了必须传入位置参数paymentForOrderID,但当前请求路径/api/payments/product-payment-add-response/没有携带该路径参数,Django无法自动注入参数导致报错。
    1. ORM查询语法错误:ProductPayment.objects.all()返回的是查询集对象,查询集没有objects属性,后续调用response_list.objects.filter()属于语法错误。
    1. 冗余参数冲突:如果采用路径传paymentForOrderID的方案,创建记录时又从请求体data['paymentForOrderID']取值,可能出现两者不一致的问题。
    1. 拼写错误:返回提示里的recieved拼写错误,正确拼写为received。
修复方案

提供两种可选方案,按需选择即可:

方案一:将paymentForOrderID作为路径参数传入

第一步:修改urls.py路由配置

在对应app的urls.py中,给接口路径加上参数匹配规则:

path('product-payment-add-response/<str:paymentForOrderID>/', views.addProductPaymentResponse, name='add-product-payment')

前端发起请求时,需要把订单ID拼在路径中,比如给订单123提交支付记录,请求路径为/api/payments/product-payment-add-response/123/

第二步:修复views.py代码

@api_view(['POST'])
@permission_classes([IsAuthenticated])
def addProductPaymentResponse(request, paymentForOrderID):
    user = request.user
    data = request.data
    # 修复ORM查询语法错误
    alreadyExists = ProductPayment.objects.filter(paymentForOrderID=paymentForOrderID).exists()
    
    if alreadyExists:
        # 修复拼写错误
        content = {'detail': 'Sorry Response has already been received'}
        return Response(content, status=status.HTTP_400_BAD_REQUEST)
    else:
        product = ProductPayment.objects.create(
                user=user,
                # 统一使用路径传入的paymentForOrderID,避免参数不一致
                paymentForOrderID=paymentForOrderID,
                paymentMethodUsed=data['paymentMethodUsed'],
                aproxTimeAndDateOfPayment=data['aproxTimeAndDateOfPayment'],
                totalAmountPaid=data['totalAmountPaid'],
                paymentDetails=data['paymentDetails'],
            )
        return Response('Payment Response added')

方案二:paymentForOrderID完全从请求体获取

不需要修改路由,直接调整视图函数参数即可:

@api_view(['POST'])
@permission_classes([IsAuthenticated])
def addProductPaymentResponse(request): # 移除位置参数声明
    user = request.user
    data = request.data
    # 从请求体获取paymentForOrderID并做非空校验
    paymentForOrderID = data.get('paymentForOrderID')
    if not paymentForOrderID:
        return Response({'detail': 'Missing paymentForOrderID parameter'}, status=status.HTTP_400_BAD_REQUEST)
    
    alreadyExists = ProductPayment.objects.filter(paymentForOrderID=paymentForOrderID).exists()
    
    if alreadyExists:
        content = {'detail': 'Sorry Response has already been received'}
        return Response(content, status=status.HTTP_400_BAD_REQUEST)
    else:
        product = ProductPayment.objects.create(
                user=user,
                paymentForOrderID=paymentForOrderID,
                paymentMethodUsed=data['paymentMethodUsed'],
                aproxTimeAndDateOfPayment=data['aproxTimeAndDateOfPayment'],
                totalAmountPaid=data['totalAmountPaid'],
                paymentDetails=data['paymentDetails'],
            )
        return Response('Payment Response added')

内容的提问来源于stack exchange,提问作者Faisal Nazik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 07:00:03