【问题标题】:Parsing JSON Response to Django DB解析对 Django DB 的 JSON 响应
【发布时间】:2020-12-25 03:26:05
【问题描述】:

我正在尝试将 Twilio 的此 Json 响应保存到我的数据库中,但不确定如何将其解析到 Django 中的模型中。目前它只是将 Json 响应输出到终端

def validate_order_with_details(request):
            try:
                client = Client(twillio_account_sid, twillio_auth_token)
                message = client.messages.create(
                    from_=twillio_from_no,
                    body=printer,
                    to=twillio_sender_mobile_no
                )
                if message:
                    return JsonResponse({"data": True, "printer": printer})
                else:
                    return JsonResponse({"data": True, "printer": "Error! while process twilio msg."})
                    print("There are an error with your transaction")
            except Exception as e:
                return JsonResponse({"data": True, "printer": str(e)})
                print("There are an error with your transaction")
        else:
            return JsonResponse({"data": False})

感谢任何帮助。

【问题讨论】:

标签: json django twilio


【解决方案1】:

我设法用下面的方法解决了这个错误。我只需要运行查询来更新我的数据库

def validate_order_with_details(request):
        try:
            client = Client(twillio_account_sid, twillio_auth_token)
            message = client.messages.create(
                from_=twillio_from_no,
                body=printer,
                to=twillio_sender_mobile_no
            )
            if message:
                # Save the Twilio SID to the database so we can look it up later to see if the message was delivered
                Order.objects.filter(id=order_id).update(twilio_sid=message.sid)
                Order.objects.filter(id=order_id).update(twilio_status=message.status)
                return JsonResponse({"data": True, "printer": printer})

            else:
                
                return JsonResponse({"data": True, "printer": "Errore! while process twilio msg."})
                print("There are an error with your transaction")
        except Exception as e:
            return JsonResponse({"data": True, "printer": str(e)})
            print("There are an error with your transaction")
    else:
        return JsonResponse({"data": False})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    相关资源
    最近更新 更多