Django Rest Framework Ordering on a SerializerMethodField
Asked Answered
D

3

29

I have a Forum Topic model that I want to order on a computed SerializerMethodField, such as vote_count. Here are a very simplified Model, Serializer and ViewSet to show the issue:

# models.py
class Topic(models.Model):
    """
    An individual discussion post in the forum
    """
    title = models.CharField(max_length=60)

    def vote_count(self):
        """
        count the votes for the object
        """
        return TopicVote.objects.filter(topic=self).count()


# serializers.py
class TopicSerializer(serializers.ModelSerializer):
    vote_count = serializers.SerializerMethodField()

    def get_vote_count(self, obj):
        return obj.vote_count()

    class Meta:
        model = Topic


# views.py
class TopicViewSet(TopicMixin, viewsets.ModelViewSet):
    queryset = Topic.objects.all()
    serializer_class = TopicSerializer

Here is what works:

  1. OrderingFilter is on by default and I can successfully order /topics?ordering=title
  2. The vote_count function works perfectly

I'm trying to order by the MethodField on the TopicSerializer, vote_count like /topics?ordering=-vote_count but it seems that is not supported. Is there any way I can order by that field?

My simplified JSON response looks like this:

{
    "id": 1,
    "title": "first post",
    "voteCount": 1
},
{ 
    "id": 2,
    "title": "second post",
    "voteCount": 8
},
{ 
    "id": 3,
    "title": "third post",
    "voteCount": 4
}

I'm using Ember to consume my API and the parser is turning it to camelCase. I've tried ordering=voteCount as well, but that doesn't work (and it shouldn't)

Dialyse answered 4/5, 2015 at 23:50 Comment(0)
T
51

This is not possible using the default OrderingFilter, because the ordering is implemented on the database side. This is for efficiency reasons, as manually sorting the results can be incredibly slow and means breaking from a standard QuerySet. By keeping everything as a QuerySet, you benefit from the built-in filtering provided by Django REST framework (which generally expects a QuerySet) and the built-in pagination (which can be slow without one).

Now, you have two options in these cases: figure out how to retrieve your value on the database side, or try to minimize the performance hit you are going to have to take. Since the latter option is very implementation-specific, I'm going to skip it for now.

In this case, you can use the Count function provided by Django to do the count on the database side. This is provided as part of the aggregation API and works like the SQL COUNT function. You can do the equivalent Count call by modifying your queryset on the view to be

queryset = Topic.objects.annotate(vote_count=Count('topicvote_set'))

Replacing topicvote_set with your related_name for the field (you have one set, right?). This will allow you to order the results based on the number of votes, and even do filtering (if you want to) because it is available within the query itself.

This would require making a slight change to your serializer, so it pulls from the new vote_count property available on objects.

class TopicSerializer(serializers.ModelSerializer):
    vote_count = serializers.IntegerField(read_only=True)

    class Meta:
        model = Topic

This will override your existing vote_count method, so you may want to rename the variable used when annotating (if you can't replace the old method).


Also, you can pass a method name as the source of a Django REST framework field and it will automatically call it. So technically your current serializer could just be

class TopicSerializer(serializers.ModelSerializer):
    vote_count = serializers.IntegerField(read_only=True)

    class Meta:
        model = Topic

And it would work exactly like it currently does. Note that read_only is required in this case because a method is not the same as a property, so the value cannot be set.

Tobolsk answered 5/5, 2015 at 1:4 Comment(0)
J
3

I will put it here because the described case is not the only one. The idea is to rewrite the list method of your Viewset to order by any of your SerializerMethodField(s) also without moving your logic from the Serializer to the ModelManager (especially when you work with several complex methods and/or related models)

def list(self, request, *args, **kwargs):
    response = super().list(request, args, kwargs)
    ordering = request.query_params.get('ordering')
    
    if "-" in ordering:      
        response.data['results'] = sorted(response.data['results'], key=lambda k: (k[ordering.replace('-','')], ), reverse=True)
    else:
        response.data['results'] = sorted(response.data['results'], key=lambda k: (k[ordering], ))

    return response
Judsen answered 20/3, 2018 at 7:28 Comment(3)
awesome! would be nice to see a fuller example with a whole viewset and a whole serializer sorting by a SerializerMethodFieldPernik
What is YourModelList if we are overriding the list method on a viewset?Pernik
is this still good if there is a django LimitOffset pagination set?Fonville
H
2

Thanks @Kevin Brown for your great explanation and answer!

In my case I needed to sort a serializerMethodField called total_donation which is the sum of donations from the UserPayments table.

UserPayments has:

  • User as a foreignKey
  • sum which is an IntegerField
  • related_name='payments'

I needed to get the total donations per User but only donations that have a status of 'donated', not 'pending'. Also needed to filter out the payment_type coupon, which is related through two other foreign keys.

I was dumbfounded how to join and filter those donations and then be able to sort it via ordering_fields.

Thanks to your post I figured it out! I realized it needed to be part of the original queryset in order to sort with ordering.

All I needed to do was annotate the queryset in my view, using Sum() with filters inside like so:

class DashboardUserListView(generics.ListAPIView):
    donation_filter =  Q(payments__status='donated') & ~Q(payments__payment_type__payment_type='coupon')
    queryset = User.objects.annotate(total_donated=Sum('payments__sum', filter=donation_filter ))
    serializer_class = DashboardUserListSerializer
    pagination_class = DashboardUsersPagination
    filter_backends = [filters.OrderingFilter]
    ordering_fields = ['created', 'last_login', 'total_donated' ]
    ordering = ['-created',]
 
Hirokohiroshi answered 14/6, 2021 at 13:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.