Django rest_framework: child object count in serializer

Viewed 6721

I need to count the number of children an object has and return this value in my API via the object serializer. I also need to count a subset of these children objects.

I have a Task object with children Asignees. In my API when I query the tasks I want to have the following data set returned:

[
    { label: "Cross the bridge",
      count_assigned: 5,
      count_completed: 3 },
    { label: "Build a fire",
      count_assigned: 5,
      count_completed: 2 }
]

How would I do this? I have found the .annotate() method but that result is not available in the serializer class.

models.py

class Task(models.Model):
    label         = models.CharField(max_length=255,null=False,blank=False)

class Assignee(models.model):
    task         = models.ForeignKey(Task, related_name='assignees', on_delete=models.CASCADE, blank=True, null=True) 
    person       = models.ForeignKey(Person, on_delete=models.CASCADE, blank=True, null=True)
    completed    = models.DateTimeField(null=True,blank=True)

serializers.py

from rest_framework import serializers

from .models import Task, Assignee
from people.serializers import PersonSerializer

class AssigneeSerializer(serializers.ModelSerializer):
    id = serializers.ReadOnlyField()
    person = PersonSerializer(read_only=True)

    class Meta:
        model = Assignee

        fields = ('id','task','person','completed')
        read_only_fields = ['id']


class TaskSerializer(serializers.ModelSerializer):
    id = serializers.ReadOnlyField()

    class Meta:
        model = Task

        fields = ('id', 'label')
        read_only_fields = ['id']
2 Answers

The proposed way

class TaskSerializer(serializers.ModelSerializer):
     id = serializers.ReadOnlyField()
     count_assigned = serializers.SerializerMethodField()
     count_completed = serializers.SerializerMethodField()

     class Meta:
         model = Task
         fields = ('id', 'label', 'count_assigned', 'count_completed')

    def get_count_assigned(self, obj):
        return obj.assignees.count()

    def get_count_completed(self, obj):
        return obj.assignees.exclude(completed__isnull=True).count()

http://www.django-rest-framework.org/api-guide/fields/#serializermethodfield

If i understand your logic correctly, you can try

in serializers

class TaskSerializer(serializers.ModelSerializer):
    count_assigned = serializers.IntegerField(read_only=True)
    count_completed = serializers.IntegerField(read_only=True)

then by queryset:

from django.db.models import Count, Case, When, IntegerField

qs = Task.objects.annotate(
        count_completed=Count(Case(
            When(assignees__completed__isnull=False, then=1),
            output_field=IntegerField(),
        ))
    ).annotate(count_assigned=Count('assignees'))

serializer = TaskSerializer(qs, many=True)

Or horribly inefficient in models:

from django.utils.functional import cached_property

class Task(models.Model):

@cached_property
def all_assignee(self):
    return self.assignees.all()

def count_assigned(self):
    return self.all_assignee.count()

def count_completed(self):
    return self.all_assignee.filter(completed__isnull=False).count()
Related