'Django - grouping in serializer

I'm struggling to create a serializer that groups the result, I can make it my "dirty" way but i am curious what is the proper way

my models:

class Station(models.Model):
    type = models.ForeignKey(StationType, on_delete=models.SET_NULL, null=True)
    line = models.CharField(max_length=1)
    number = models.CharField(max_length=3)
    bool1= models.BooleanField( null=True, blank=True, default=False)
    bool2= models.BooleanField( null=True, blank=True, default=False)
    def str(self):return self.number

my serializer:

class mySerializer(serializers.ModelSerializer):

    class Meta:
        model = Station
        fields = ['line', 'number','bool1', 'bool2']

result:

[
    {
        "line": "1",
        "number": "001",
        "bool1": false,
        "bool2": false
    },
    {
        "line": "1",
        "number": "001",
        "bool1": false,
        "bool2": false
    },
    {
        "line": "3",
        "number": "013",
        "bool1": false,
        "bool2": false
    }
]

expected result (more or less):

[
    {"line": "1",{
        "number": "001",
        "bool1": false,
        "bool2": false
    },
    {
        "line": "1",
        "number": "001",
        "bool1": false,
        "bool2": false
    }},
    {"line": "3",
    {
        "number": "013",
        "bool1": false,
        "bool2": false
    }}
]

I would be grateful for all kinds of help ;) If you could give me the source where I could learn more about serializers I would be even more grateful



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source