How to Join and Aggregate in Django REST Framework

I'm quite new to Django and I'm currently stuck on the following problem.

I have models as:

class City(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=100)
    area = models.FloatField()


class NoiseData(models.Model):
    location = models.CharField(max_length=100)
    city_id = models.ForeignKey(City,
                                on_delete=models.CASCADE)
    noise_type = models.CharField(max_length=30)

I would like to have data aggregations by city in my api in form like:

[
        {
                "id": 1,
                "name": "New York",
                "count": 198, # this would be count of NoiseData,
                "count_area_normalized": 0.198 # this would be count of NoiseData / area of City,
                "noise_type_metadata": "{\"Loud Music/Party\": 167, \"Banging/Pounding\": 21, \"Loud Talking\": 9, \"Loud Television\": 1}", # ideally want a JSON in which I can show count for each noise_type in the city
           }

Thanks a lot in advance!

For the count field and others that need computation you should use SerializerMethodField.

from rest_framework import serializers
from django.db.models import Count

class CitySerializer(serializers.ModelSerializer):
    count = serializers.SerializerMethodField()
    count_area_normalized = serializers.SerializerMethodField()
    noise_type_metadata = serializers.SerializerMethodField()

    class Meta:
        model = City
        fields = ('name',)

    def get_count(self, obj):
        return NoiseData.objects.filrer(city_id=obj).count()

    def get_count_area_normalized(self, obj):
        return get_count(self, obj)/obj.area
   
    def get_noise_type_metadata(self,obj):
        return {data.noise_type:data.count for data in NoiseData.objects.filrer(city_id=obj).values('noise_type').annotate(count=Count('pk'))}

This should work for you but, to improve the performance I would compute al this data before in the DB using the ORM.

Back to Top