Home > Blockchain >  Invalid literal int() with base 10: '[' when doing a filter with request.body
Invalid literal int() with base 10: '[' when doing a filter with request.body

Time:05-02

def patch(self, request):
        Claim.objects.filter(id__in=request.body, presented_to_client=False).update(presented_to_client=True, presented_to_client_date=datetime.datetime.now())
        return HttpResponse(status=200)

I'm trying to update some of my objects this way. But I get

invalid literal for int() with base 10: '['

so I thought I might need to cast my request.body to list, and I tried it this way:

def patch(self, request):
        Claim.objects.filter(id__in=list(request.body), presented_to_client=False).update(presented_to_client=True, presented_to_client_date=datetime.datetime.now())
        return HttpResponse(status=200)

and I still get the same error.

Why is this happening? If I hardcode it this way:

id__in=[8]

I don't get any errors.

Thanks.

CodePudding user response:

request.body is a binary string that contains [8], not a list with an int. You can parse it as JSON with json.loads(…) [Python-doc]:

from django.db.models import Now
import json

def patch(self, request):
    Claim.objects.filter(
        id__in=json.loads(request.body),
        presented_to_client=False
    ).update(
        presented_to_client=True,
        presented_to_client_date=Now()
    )
    return HttpResponse(status=200)
  • Related