1

When I request for order that contains multiple order items and after filter data from the database at the time of return response my order item id gets changed it contains id that even didn't exits in the database.

def list_order_by_entrepreneur_view(request):
order = Order.objects.distinct().filter(orderitem__entrepreneur_id=request.data['entrepreneur_id'],
                                        orderitem__status=request.data['status'])
serializer = ListOrderSerializer(order, many=True)
for order in serializer.data:
    for order_item in order['order_items']:
        print(order_item['order_item_id'])
return Response(serializer.data)

In the above printing statement it will return the correct order item ids like this

41750862938521900
48770276682006700
64456048092798900
94735086015354602
94735086015354601
14875991458541102
14875991458541101
84042205998714300

But when I return the response to client side then the id '94735086015354601' will convert to '94735086015354610' and same for id '94735086015354602'. I don't know why.

1 Answers1

0

This likely is wrong at the JavaScript side. Before 2018, all numbers in JavaScript were represented with a 64-bit floating point. Now there is a BigInt to work with, but likely not when JSON deserializing. This has a mantisse of 53-bit, so it can represent integer numbers correctly up to 253-1, so 9'007'199'254'740'992, since a floating point uses a sign-magnitude representation.

This thus means that 94'735'086'015'354'601, which is more than ten times larger, can not be precisely represented anymore with such representation, and therefore will be rounded to the nearest representable floating point number.

You thus might want to send strings instead of numbers as a response, something similar like:

serializer = ListOrderSerializer(order, many=True)
response = [
    {**order, 'order_item_id': list(map(str, order['order_item_id']))}
    for order in serializer.data
]
return Response(response)
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555