ValueError: invalid literal for int() with base 10

ValueError: invalid literal for int() with base 10

我很难理解为什么自定义 uuid 文本字段出现此错误。

数据:

{
            "contact_uuid": "49548747-48888043",
            "choices": "",
            "value": "1",
            "cardType": "TEXT",
            "step": 1,
            "optionId": "",
            "path": "/app/e120703e-2079-4e5f-8420-3924f9e0b9c8/page1/next",
            "title": "Welcome to my app"
        }

查看: 这里重要的一点是保存数据的行,因此您可以忽略其余代码。

class NextDialog(generics.GenericAPIView):
    permission_classes = (permissions.AllowAny,)

    def post(self, request, *args, **kwargs):
        data = request.data
        contact_uuid = data["contact_uuid"]
        step = data["step"]
        value = data["value"]
        optionId = data["optionId"]
        path = get_path(data)
        request = build_rp_request(data)
        app_response = post_to_app(request, path)
        response_data = AppAssessment(contact_uuid, step, value, optionId)
        response_data.save()
        try:
            report_path = app_response["_links"]["report"]["href"]
            response = get_report(report_path)
            return Response(response, status=status.HTTP_200_OK)
        except KeyError:
            pass
        message = format_message(app_response)
        return Response(message, status=status.HTTP_200_OK)

型号:

class AppAssessment(models.Model):
    contact_uuid = models.CharField(max_length=255)
    step = models.IntegerField(null=True)
    optionId = models.IntegerField(null=True, blank=True)
    user_input = models.TextField(null=True, blank=True)
    title = models.TextField(null=True, blank=True)
    # description = models.TextField(null=True, blank=True)

    def __str__(self):
        return self.contact_uuid

当 response_data.save() 行在视图中运行时,出现以下错误:

  File "/home/osboxes/ndoh-hub/venv/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 972, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: '49548747-48888043'

我认为这里的问题是参数的顺序。当您使用位置参数调用 AppAssessment() 时,参数的顺序可能不是您所期望的。所以用关键字参数调用 AppAssessment() 可能会解决问题。

替换此行:

response_data = AppAssessment(contact_uuid, step, value, optionId)

有了这个:

response_data = AppAssessment(contact_uuid=contact_uuid, step=step, optionId=optionId, user_input=value)

我不确定您想 value 用于哪个属性。但我猜是 user_input.