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

def patch(self, request):
        Claim.objects.filter(id__in=request.body).update(....)
        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)).update(...)
        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.



Solution 1:[1]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Willem Van Onsem