我有3个序列化器:
class abcSerializer(serializers.ModelSerializer):
created=Field(source='time')
result = Field(source='result')
class Meta:
model = abc
fields = ('created','result')
order_by = ['-created']
class defSerializer(serializers.ModelSerializer):
created=Field(source='time')
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
fields = ('obj1','obj2')
在views.py中:
data = main.objects.filter(id=111)
serializer = mainSerializer(data)
return Response(record)
在UI中,我正在显示序列化数据,如下所示:
created result
abc 1-9-2016 9:30:00 pass
def 2-9-2016 10:34:00 fail
我收到的序列化数据如下:
data = [{'created':'1-09-2106','result':'pass'},{'created':'2-09-2106','result':'fail'}]
我想根据最新日期 创建字段对数据进行排序。
created result
def 2-9-2016 9:30:00 fail
abc 1-9-2016 10:34:00 pass
无法弄清楚如何对创建的字段进行排序 任何帮助将不胜感激!!
答案 0 :(得分:0)
我认为您希望在模型中使用order
而不是序列化器。
class abcModel(models.Model):
created = models.DateTimeField(...)
result = ...
class Meta:
ordering = ['-created']