Hi everyone,
I'm rather new to Django development so please excuse me if this sounds trivial.
I need to build an API for some data for which there is no model. The reason behind this is because the data required from that one call is scattered across several databases. To accomplish this, I wrote a functional view similar to
@api_view(['GET'])
def FleetLocationDataList(request):
if request.method == 'GET':
all_location_ps_dicts = get_all_ps_location_dicts()
# all_location_obj = [FleetLocationData(**location_ps_dict)
# for location_ps_dict
# in all_location_ps_dicts]
serializer = serializers.FleetLocationDataSerializer(all_location_ps_dicts, many=True)
return Response(serializer.data)
Where get_all_ps_location_dicts() returns a dictionary of fields. I defined the serializer as:
class FleetLocationDataSerializer(serializers.Serializer):
field1 = serializers.IntegerField(read_only=True)
field2 = serializers.CharField(read_only=True)
field3 = serializers.CharField(read_only=True)
field4 = serializers.CharField(read_only=True)
class Meta:
model = None
I defined the corresponding entries in urls.py and and everything works fine. However, the response content returned is in a slightly different format than the generic ones:
Generic:
Mine:
{
"field1": 2,
"field2": "S-0000021",
}
} ... etc
For consistency on the client end I would like to make mine have the same formatting, as well as the ability to split into pages. How would I go about doing that? I'm guessing that its something in the way serializer.Serializer handles input types?
Thanks a bunch!