如何序列化下面的queryset结果?

时间:2019-07-08 11:22:11

标签: django django-models django-rest-framework

我使用django rawquery从数据库获取结果并将其序列化如下:

 class arizakestirimi_func(ListAPIView):    
 serializer_class = arizakestirimiserializer
 def get_queryset(self):
    queryset = isyeriarizabilgileri.objects.raw("""
        SELECT
        M.id as id,
        DATE_PART('day',(M.tarih)::timestamp - (D.mindate)::timestamp) *24 +DATE_PART('hour',(M.tarih)::timestamp - (D.mindate)::timestamp) +
        DATE_PART('minute',(M.tarih)::timestamp - (D.mindate)::timestamp) / 60 as datediff
        FROM arizakestirimi_isyeriarizabilgileri M
        INNER JOIN
        (SELECT DISTINCT ON (isyeri) isyeri,id as id,durustahmini,tarih as mindate
     FROM arizakestirimi_isyeriarizabilgileri 
     WHERE durustahmini='MEKANIK ARIZA' AND isyeri='15400001'
     ORDER BY isyeri, tarih ASC) D
        ON M.isyeri = D.isyeri AND M.durustahmini = D.durustahmini
        ORDER BY M.tarih ASC

      """)
    return queryset

Serializer类如下所示:

class arizakestirimiserializer(serializers.Serializer):

    isyeri = serializers.CharField(max_length=30)
    datediff= serializers.FloatField()

结果json。我使用ListAPIView显示json:

[
    {
        "isyeri": "15400001",
        "datediff": 2520.0
    },
    {
        "isyeri": "2520.0",
        "datediff": 704.0
    },
    {
        "isyeri": "15400001",
        "datediff": 700.0
    }
]

我想获取json:

{
    "15400001":[
               {"2520.0"}, 
                {"700.0"},
                {"704.0"}
]
}

谢谢

0 个答案:

没有答案