Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort serialized fields with Django Rest Framework

I'm looking for suggestions on how to sort/group serialized fields by value. Here's a code example explaining what I want to achieve.

Models

class Folder(models.Model):
    name = models.CharField()


class File(models.Model):
    filetype = models.CharField()
    name = models.CharField()
    folder = models.ForeignKey(Folder)

Serializers

class FileSerializer(serializers.ModelSerializer):

    class Meta:
        model = File
        fields = ('id', 'filetype', 'name')


class FolderSerializer(serializers.ModelSerializer):

    files = FileSerializer(read_only=True)

    class Meta:
        model = Folder
        fields = ('name', 'files')

This serializes to:

{
    "name": "Test Folder",
    "files": [
        {"id": 1, "filetype": "pdf", "name": "some pdf file"}.
        {"id": 2, "filetype": "pdf", "name": "some other pdf file"},
        {"id": 3, "filetype": "txt", "name": "some text file"}
    ]
}

I'm looking for a way to serialize to this:

{
    "name": "Test Folder",
    "files": [
        "pdf": [
            {"id": 1, "name": "some pdf file"},
            {"id": 2, "name": "some other pdf file"}
        ],
        "txt": [
            {"id": 3, "name": "some text file"}
        ]
    ]
}
like image 791
Andreas Avatar asked Jul 11 '17 11:07

Andreas


1 Answers

Try to use SerializerMethodField for this. You need to implement something like this:

class FolderSerializer(serializers.ModelSerializer):
    files = serializers.SerializerMethodField()

    class Meta:
        model = Folder
        fields = ('name', 'files')

    def get_files(self, obj):
        result = {'pdf': [], 'txt':[]}
        for file in obj.file_set.all():
            serializer = FileSerializer(file)
            if file.name.endswith('pdf'):
                  result['pdf'].append(serializer.data)
            if file.name.endswith('txt'):
                  result['txt'].append(serializer.data)   
        return result
like image 58
neverwalkaloner Avatar answered Oct 01 '22 02:10

neverwalkaloner