Search code examples
djangoserializationdjango-rest-frameworkgrouping

Django rest framework filtering and grouping items in serializer


I have models like this:

class schoolCycle(models.Model):
    name = models.CharField(max_length=255)
    code = models.CharField(max_length=255)

class measurements(models.Model):
    name = models.CharField(max_length=255)
    weight = models.FloatField()
    school_cycle_id = models.ForeignKey(schoolCycle,on_delete=models.DO_NOTHING, related_name='measurements')
    code = models.CharField(max_length=255)

class aspects(models.Model):
    name = models.CharField(max_length=255)
    code = models.CharField(max_length=255)
    measurement_id = models.ForeignKey(measurements,on_delete=models.DO_NOTHING, related_name='aspects')

class standards(models.Model):
    name = models.CharField(max_length=255)
    code = models.CharField(max_length=255)
    weight = models.FloatField()
    aspect_id = models.ForeignKey(aspects,on_delete =models.DO_NOTHING, related_name='standards')

as can be seen, "standard" refers an 'aspect' and aspect refers 'a measurement" and measurement refers a 'school cycle' as foreign key. in want to create a seriallizer like this:

  [{ "measurement 1" : { 
                   "school cycle 1" :{
                                      "aspect 1": {
                                                     {"standard 1" : ...},
                                                     {"standard 2" : ...},
                                                   }

                                      "aspect 2": {
                                                     {"standard 3" : ...},
                                                     {"standard 4" : ...},
                                                   }
                                      }
                  "school cycle 2" :{
                                      "aspect 3": {
                                                     {"standard 5" : ...},
                                                     {"standard 6" : ...},
                                                   }

                                      "aspect 4": {
                                                     {"standard 7" : ...},
                                                     {"standard 8" : ...},
                                                   }
                                      }
  "measurement 2" : { 
                   "school cycle 3" :{
                                      "aspect 5": {
                                                     {"standard 9" : ...},
                                                     {"standard 10" : ...},
                                                   }

                                      "aspect 6": {
                                                     {"standard 11" : ...},
                                                     {"standard 12" : ...},
                                                   }
                                      }
                  "school cycle 4" :{
                                      "aspect 7": {
                                                     {"standard 13" : ...},
                                                     {"standard 14" : ...},
                                                   }

                                      "aspect 8": {
                                                     {"standard 15" : ...},
                                                     {"standard 16" : ...},
                                                   }
                                      }

     }]

Meaning i want to serialize by grouping aspects that have the same school cycle and measurement under their own key.

How to achieve this kind of grouping in Django rest framework? Thank you in advance for your help.


Solution

  • from rest_framework import serializers
    
    class SchoolCycleSerializer(serializers.ModelSerializer):
        measurements = MeasurementsSerializer(read_only=True, many=True)
    
        class Meta:
            model = schoolCycle
            fields = '__all__'
    
    class MeasurementsSerializer(serializers.ModelSerializer):
        aspects = AspectsSerializer(read_only=True, many=True)
    
        class Meta:
            model = measurements
            fields = '__all__'
    
    class AspectsSerializer(serializers.ModelSerializer):
        standards = StandardsSerializer(read_only=True, many=True)
    
        class Meta:
            model = aspects
            fields = '__all__'
    
    class StandardsSerializer(serializers.ModelSerializer):
        class Meta:
            model = standards
            fields = '__all__'
    
    # note that it's good practice to name your models starting with a capital letter.