43

I'm using Django Rest Framework and need to add extra data to a result set. Specifically, where you would usually have:

{
    "count": 45, 
    "next": "http://localhost:8000/foo/bar?page=2", 
    "previous": null, 
    "results": [
        {...}
    ]
}

I would like to add extra counts like so:

{
    "count": 45,
    "10_mi_count": 10,
    "20_mi_count": 30,
    "30_mi_count": 45,
    "next": "http://localhost:8000/foo/bar?page=2", 
    "previous": null, 
    "results": [
        {...}
    ]
}

The extra counts in this example are just how many of the objects have a field distance with a value less than the miles described in the key.

My issue is that I have no idea where the best place to extend and insert this behaviour is.

Ideally I'd like this to work whether or not the results are paginated, making no assumptions.

What I'm really after here is a nod in the right direction (and why that is the right place for it go).

I've checked the docs and can't find anything that describes how to add things like this, but I would be more than happy to be proven wrong on that score.

Tom Manterfield
  • 6,515
  • 6
  • 36
  • 52

5 Answers5

51

Since you seem to be using one of the ListViews from the Rest Framework, you could override the list() method in your class and set new values on the resulting data, like this:

    def list(self, request, *args, **kwargs):
        response = super().list(request, args, kwargs)
        # Add data to response.data Example for your object:
        response.data['10_mi_count'] = 10 # Or wherever you get this values from
        response.data['20_mi_count'] = 30
        response.data['30_mi_count'] = 45
        return response

Notice that your class must inherit the ListModelMixin directly or via a GenericView from the Rest Framework API (http://www.django-rest-framework.org/api-guide/generic-views#listmodelmixin). I really don't know if it is the right way to do this, but it is a quick fix.

Hope it helps!

Artem Bernatskyi
  • 4,185
  • 2
  • 26
  • 35
gaizka
  • 711
  • 6
  • 3
  • Hey, I went with a different method in the end which I've described in my own answer. This is a good alternative though so thanks very much. – Tom Manterfield Jun 16 '14 at 13:02
  • I did a variation of this that worked great. I wanted some aggregate min-max data returned with my query for some slider ranges. I copied the implementation of the list method from the parent class completely into my subclass. It gets the queryset object so i could modify it to run aggregate on the query set and add the results to the response. thanks! – t1m0 Feb 28 '17 at 23:56
11

Use SerializerMethodField as mentioned in this solution.

It can be used to add any sort of data to the serialized representation of your object. (REST framework doc)

Example from the documentation:

from django.contrib.auth.models import User
from django.utils.timezone import now
from rest_framework import serializers

class UserSerializer(serializers.ModelSerializer):
    days_since_joined = serializers.SerializerMethodField()

    class Meta:
        model = User

    def get_days_since_joined(self, obj):
        return (now() - obj.date_joined).days
suhailvs
  • 20,182
  • 14
  • 100
  • 98
user42488
  • 1,140
  • 14
  • 26
  • 1
    A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](http://meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted](http://stackoverflow.com/help/deleted-answers). – mrun Sep 13 '17 at 14:29
5

In the end I just created a custom pagination serializer with a field like so:

class DistanceCountField(serializers.Field):
    def to_native(self, value):
        try:
            distance_counts = {
                '1_mile': self._count_lte_miles(value, 1),
                '5_mile': self._count_lte_miles(value, 5),
                '10_mile': self._count_lte_miles(value, 10),
                '20_mile': self._count_lte_miles(value, 20),
            }
        except FieldError:
            distance_counts = None

        return distance_counts

    def _count_lte_miles(self, value, miles):
        meters = miles * 1609.344
        return value.filter(distance__lte=meters).count()


class PaginatedCountSerializer(pagination.PaginationSerializer):
    distance_counts = DistanceCountField(source='paginator.object_list')

    class Meta:
        # Class omitted, just a standard model serializer
        object_serializer_class = MyModelSerializer 

I also added a distance annotation to each object in the queryset for the filtering to work on.

Tom Manterfield
  • 6,515
  • 6
  • 36
  • 52
3

I'm piggybacking off the accepted answer here!

I think the main issue is that the serializer returns a ReturnList, which is just a regular list with some extra context.

You can just replace the data with your own OrderedDict like so:

response = super().list(request, *args, **kwargs)
new_data = OrderedDict()
new_data.update({
    'results': response.data,
    '10-mi-count': 10,
    #etc...
})
response.data = new_data

return response 
2

I used DRF with pagination as well and wanted to add a summary of the returned queryset itself, so first I had to get the queryset in the list method.

def list(self, request, *args, **kwargs):
    queryset = self.filter_queryset(self.get_queryset())
    summary = {}
    summary['x'] = queryset.filter(transaction_type='x').count()
    summary['y'] = queryset.filter(transaction_type='y').count()

    page = self.paginate_queryset(queryset)
    if page is not None:
        serializer = self.get_serializer(page, many=True)
        data = {}
        data['summary'] = summary
        data['details'] = serializer.data
        return self.get_paginated_response(data)


    serializer = self.get_serializer(queryset, many=True)
    data = {}
    data['summary'] = summary
    data['details'] = serializer.data
    return Response(data)

This gave me the following response:

{
  "count": 20,
  "next": "http://..",
  "previous": null,
  "results": {
    "summary": {
      "x": 15,
      "y": 5,
    },
    "details": [
      {.....

Note: this will work for non-paginated results as well.

Mitalee Rao
  • 191
  • 6