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"}
]
]
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With