My problem is with Django RestFramework and describes how to group elements.
This is my serializers.py
from collaborativeAPP.models import * from rest_framework import serializers class VocabSerializer(serializers.ModelSerializer): term_word = serializers.CharField(source='term.word',read_only=True) kwdGroup = serializers.StringRelatedField() class Meta: model = Vocab fields = ('id','term_word', 'meaning','kwdGroup') class TermSerializer(serializers.ModelSerializer): word = serializers.CharField(read_only=True) class Meta: model = Term fields = ('url', 'word')
The following json is the actual result:
{"results":[ { "id": 5, "term_word": "word1", "meaning": "Text1" "kwdGroup": "A" }, { "id": 6, "term_word": "word2", "meaning": "Text2" "kwdGroup": "A" }, { "id": 7, "term_word": "word3", "meaning": "Text3" "kwdGroup": "A" } ]}
As you can see, "kwdGroup" is a repeating element that I have to group.
I would like to group by kwdGroup
{"A":[ { "id": 5, "term_word": "word1", "meaning": "Text1" }, { "id": 6, "term_word": "word2", "meaning": "Text2" }, { "id": 7, "term_word": "word3", "meaning": "Text3" } ] }
I am looking for answers at http://www.django-rest-framework.org/ in the api manual, but I am having difficulty finding an approach to it. Do you share the same problem? Do you have any suggestion on how I can do this? Do you have an example that concerns grouping elements using Django RestFramework?
Thanks in advance.