Views/Serializer for Non Model-Based Serializers

778 views
Skip to first unread message

Simon Han

unread,
Nov 5, 2015, 7:42:26 PM11/5/15
to Django REST framework
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:

{
    "count": 2,
    "next": null,
    "previous": null,
    "results": [
        {
            "url": "http://127.0.0.1:8000/users/1/",
            "username": "shan",
            "snippets": [
                "http://127.0.0.1:8000/snippets/1/"
            ]
        },
    ]
}
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!

aRkadeFR

unread,
Nov 6, 2015, 3:35:52 AM11/6/15
to django-res...@googlegroups.com
Hello,

The difference between your results and the generic one is only
the pagination? Or the sorting of your fields etc.?

If it's the pagination, you should call self.paginate_queryset. You may
adapt your code to be compliant with the API (which I don't know
in depth :D).

http://www.django-rest-framework.org/api-guide/viewsets/#marking-extra-actions-for-routing
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
aRkadeFR
Message has been deleted

Simon Han

unread,
Nov 9, 2015, 12:48:17 PM11/9/15
to Django REST framework
Yes, pagination was what I wanted and I managed to get it working. Thanks for pointing me in the right direction! 

For anyone else who wants to do the same thing, define the view as a generics.GenericAPIView, and overwrite the get_queryset() function to return the results of your custom SQL call. This gives you all the functionality of a generics.GenericAPIView, which includes pagination. For example,

class SnippetList(mixins.ListModelMixin,
                  generics.GenericAPIView):
    serializer_class = SnippetSerializer

    def get_queryset(self):
        return get_SQL_query_results_as_dict()

Miguel Veces

unread,
Jul 14, 2017, 10:46:24 AM7/14/17
to Django REST framework
hola no entendi bien tu solucion podrias explicarme un poco mas?
Reply all
Reply to author
Forward
0 new messages