Home > front end >  ValueError: invalid literal for int() with base 10
ValueError: invalid literal for int() with base 10

Time:04-05

I'm struggling to understand why I have this error for the custom uuid text field.

Data:

{
            "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"
        }

View: The important bit here is the line that saves the data so you can gloss over the rest of the code.

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)

Model:

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

When the response_data.save() line runs in the view, I get the following error:

  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'

CodePudding user response:

I think the problem here is the order of arguments. when you call AppAssessment() with positional arguments, the order of arguments may not be what you expect. so calling AppAssessment() with keyword arguments may solve the problem.

replace this line:

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

with this:

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

I am not sure which attribute you wanted to use value for. but I guess it was user_input.

  • Related