Django RestFramework分组者 [英] Django RestFramework group by
问题描述
我的问题与Django RestFramework有关,并且有关如何对元素进行分组.
My issue is related to Django RestFramework and is about how to group elements.
这是我的 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')
以下json是实际结果:
The following json it's 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"
}
]}
您可能会注意到,"kwdGroup"是要对其进行分组的重复元素.
As you can notice "kwdGroup" is a repetitive element that i which to group.
我想按kwdGroup分组
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"
}
]
}
我正在寻找 http://www.django-rest-framework.org/上的答案在api指南上,但我很难找到一种方法来领导它. 您是否也遇到同样的问题?您有什么建议吗?您是否有使用Django RestFramework处理元素分组的示例?
I'm looking for answers on http://www.django-rest-framework.org/ on api guide but i'm having difficulties to find an approach to lead with it. Do you share this same issue? Do you have any suggestion how can i do this? Do you have any example that deals with element grouping using Django RestFramework?
谢谢.
推荐答案
我们假定kwdGroup
字段是与名为KeyWordGroup
的模型的关系字段.
Let's assume that the kwdGroup
field is the relation field to a model called KeyWordGroup
.
默认ListSerializer
使用to_representation
方法呈现序列化对象的列表
The default ListSerializer
uses the to_representation
method to render a list of the serialized objects rest_framework :
class ListSerializer(BaseSerializer):
...
def to_representation(self, data):
"""
List of object instances -> List of dicts of primitive datatypes.
"""
# Dealing with nested relationships, data can be a Manager,
# so, first get a queryset from the Manager if needed
iterable = data.all() if isinstance(data, models.Manager) else data
return [
self.child.to_representation(item) for item in iterable
]
我们可以修改ListSerializer
以将结果分组,例如:
We can modify the ListSerializer
to group the results for example:
class VocabListSerializer(serializers.ListSerializer):
def to_representation(self, data):
iterable = data.all() if isinstance(data, models.Manager) else data
return {
kwdGroup: super().to_representation(Vocab.objects.filter(kwdGroup=kwdGroup))
for kwdGroup in KeyWordGroup.objects.all()
}
然后我们可以将修改后的VocabListSerializer
与VocabSerializer
一起使用.
We can then use the modified VocabListSerializer
with the VocabSerializer
.
class VocabSerializer(serializers.Serializer):
...
class Meta:
list_serializer_class = VocabListSerializer
这篇关于Django RestFramework分组者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!