How to Count Reverse ManyToMany Relationships with Django REST Framework

Django Django
Django REST Framework Django REST Framework

Sometimes it's useful to count the numbers of reverse m2m relationships for each specific object of a queryset.

Normally with Django you would do this at the view level and it would automatically be available for you in the templates, through Django's template engine.

#views.py

from django.db.models import Count

queryset = Movie.objects.annotate(num_actors=Count('actors'), num_comments=Count('comments'), num_likes=Count('likes')).order_by('-created_at')

In the view above, num_actors specifies the field, and actors specifies the reverse m2m, in this case with a related_name='actors' specified at the model level.

.annotate(num_actors=Count('actors')

Without a specified reletade_name at the model level you can still reach and count the actors with actor_set.

.annotate(num_actors=Count('actor_set')

At the surface we can achieve the same results With Django REST Framework by specifying a read_only IntegerField's with a source to the reverse ManytoMany relationship.

#serializers.py

class MovieListSerializer(serializers.ModelSerializer):
    num_actors = serializers.IntegerField(source='actors.count', read_only=True)
    num_comments = serializers.IntegerField(source='comments.count', read_only=True)
    num_likes = serializers.IntegerField(source='likes.count', read_only=True)

    class Meta:
        model = Movie
        fields = [
            'created_at',
            'updated_at',
            'title',
            'slug',
            'image',
            'description',
            'num_actors',
            'num_comments',
            'num_likes',
        ]

However, there is a problem with this solution. It will introduce an N+1 problem where the database is hit for every result that is returned, simply put,  it does not scale well. As the database grows so does the time it takes to fetch objects from your API.

What we need to use a combination of the two and use Annotations (as was used in the regular Django example). So instead of using the serializer to find the source we simply specify the IntegerFields as is, and annotate in the view.

#serializers.py

class MovieListSerializer(ListAPIView):
    num_actors = serializers.IntegerField(read_only=True)
    num_comments = serializers.IntegerField(read_only=True)
    num_likes = serializers.IntegerField(read_only=True)

    class Meta:
        model = Movie
        fields = [
            'created_at',
            'updated_at',
            'title',
            'slug',
            'image',
            'description',
            'num_actors',
            'num_comments',
            'num_likes',
        ]

As you would do normally in Django, we annotate and Count each relationship at the view-level.

#views.py

from django.db.models import Count

queryset = Movie.objects.annotate(num_actors=Count('actors'), num_comments=Count('comments'), num_likes=Count('likes')).order_by('-created_at')

It is always recommended to specify related_name at the model level as such:

class Actor(models.Model):
    created_by          = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True)
    updated_by          = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, blank=True, related_name="+")
    created_at          = models.DateTimeField(auto_now_add=True)
    updated_at          = models.DateTimeField(auto_now=True)
    title               = models.CharField(max_length=120)
    slug                = AutoSlugField(populate_from='title', unique=True, max_length=120)
    image               = models.ImageField(upload_to='actors', null=True, blank=True)
    description         = RichTextField(max_length=2000, null=True, blank=True)
    movies            	= models.ManyToManyField('cinema.Movie', related_name='actors')

    class Meta:
        verbose_name = 'Actor'
        verbose_name_plural = 'Actors'

    def __str__(self):
        return self.title
movies = models.ManyToManyField('cinema.Movie', related_name='actors')

The related name makes it easy to query your data in a more readable way. We can now reach the relationship with movies.actors:

#like the following examples:
movies.actors
num_actors = serializers.IntegerField(source='actors.count', read_only=True)
queryset = Movie.objects.annotate(num_actors=Count('actors'), num_comments=Count('comments'), num_likes=Count('likes')).order_by('-created_at')

#instead of the following examples:
movies.actor_set
num_actors = serializers.IntegerField(source='actor_set.count', read_only=True)
queryset = Movie.objects.annotate(num_actors=Count('actor_set'), num_comments=Count('comment_set'), num_likes=Count('like_set')).order_by('-created_at')

Did You Enjoy the Tutorial?

Consider sending some love to Freddie the creator.

Comments (0)

You need to be logged in to comment..

Topics Mentioned in this Tutorial

Django
7
0
7
Django is a web application framework written in Python. The framework was crafted by developers for developers to make it easier to build better web applications with less code, Hence  Django's slogan "The web framework for perfectionists with deadlines. Besides being a framework for developers that want to ship to production, Django also sports one of the, if not the best documentation found for any web application framework....
Django REST Framework
6
0
1
Django REST Framework is a powerful package for building REST API's with the Django Web Framework and Python. Django REST Framework comes with a browsable API out of the box, a feature that improves the developer experience dramatically. If you have developed applications with Django before, you will feel right at home as Django REST Framework is model after Django. You can query your database with Django's ORM and that you have come accustomed to, and with ease serialize that data as JSON....