我正在使用Django rest framewrok,我的数据存储在数据库中,如下所示。这里的“名称”列包含不同的组织名称,我正在尝试根据不同的组织名称对数据进行分组并创建json响应,如下所示。
Json:
[
{
"org1": [
{
"address": "france",
"phone": "3"
},
{
"address": "uk",
"phone": "4"
},
{
"address": "ind",
"phone": "99"
}
]
},
{
"org2": [
{
"address": "Uk",
"phone": "8997"
}
]
}
]
模型,序列化器和视图集如下所示
class OrganizationSerializer(serializers.ModelSerializer):
class Meta:
model = CustomerEvents
fields = ('name','address','phone',)
class OrganizationSerializerViewSet(generics.ModelViewSet):
queryset = Organization.objects.all()
serializer_class = OrganizationSerializer
class Organization(models.Model):
name = models.CharField(max_length=255, blank=False)
address = models.CharField(max_length=255, blank=False, null=False)
phone = models.CharField(max_length=255, blank=False, null=False)
class Meta:
ordering = ('-pk',)
db_table = 'org'
请帮助我如何使用DRF做到这一点?
谢谢