6

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"}
        ]
    ]
}

1 Answer 1

6

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.