嵌套表示:
[
{
'id': 1,
'phone': 1023456789
'stats': {
'created': 1-09-2016,
'result': 'fail',
...
},
},
{
'id': 2,
'phone': 1023456789
'stats': {
'created': 13-09-2016,
'result': 'pass',
},
},
]
我有3个序列化器:
class abcSerializer(serializers.ModelSerializer):
created=Field(source='created')
result = Field(source='result')
class Meta:
model = abc
fields = ('created','result')
order_by = ['-created']
class defSerializer(serializers.ModelSerializer):
created=Field(source='created')
result = Field(source='result')
class Meta:
model = def
fields =('created','result')
order_by = ['-created']
class mainSerializer(serializers.ModelSerializer):
obj1 = abcSerializer()
obj2 = defSerializer()
class Meta:
model = main
'''
main is model class.Contains fields as id,phone,stat
'''
fields = ('obj1','obj2')
在views.py
中访问这些序列化数据:
data = main.objects.filter(id=111)
serializer = mainSerializer(data)
return Response(record)
我希望根据字段“创建”按降序对已分类的数据进行排序。
尝试了一些SO链接&然后github链接为link但没有成功。 github链接类似于我的问题,但它没有解决我的解决方案。
有人可以指出解决我问题的方法!!