django 使用序列化器从API得到json格式的响应

xlpyo6sf  于 2023-01-14  发布在  Go
关注(0)|答案(1)|浏览(174)

我一直试图得到下面给出的响应,但有点卡住了,至于如何实现它。。我尝试了其他方法没有序列化,但我的分页不工作的预期。
分页问题的问题是Pagination does not seem to be working using get method and APIView
下面是我的数据集:

data = [['1','2023-01-04 12:39','{"kpiThreshold": 23, "kpiValue": 25, "kpiDiff": 2}'],
        ['2','2023-01-03 12:39','{"kpiThreshold": 23, "kpiValue": 26, "kpiDiff": 3}'],
        ['1','2023-01-02 12:39','{"kpiThreshold": 23, "kpiValue": 27, "kpiDiff": 4}']]
    df_new = pd.DataFrame(data, columns=['id', 'date','kpi'])
    df_new

serializer.py

from rest_framework import serializers
from pegaapi.models import PegaAlerts

class AlertsSerializer(serializers.ModelSerializer):

    class Meta:
        model = PegaAlerts
        fields = ('id','generateddatetime','kpiValues')

views.py

class FullLogsAPI(GenericAPIView):
    pagination_class=CustomPagination
    serializer_class = AlertsSerializer
    def get(self,request, *args, **kwargs):
        envid = self.kwargs.get('envid')
        nodeid = self.kwargs.get('nodeid')
        startdatetime = self.request.GET.get('startdatetime')
        enddatetime = self.request.GET.get('enddatetime')
        filter_list=PegaAlerts.objects.filter(envId=envid, serverId=nodeid, generateddatetime__lte=enddatetime, generateddatetime__gte=startdatetime,).order_by('generateddatetime')
        page = self.paginate_queryset(filter_list)
        serializer = AlertsSerializer(page, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

回复:

[
    {
        "id": 1,
        "generateddatetime": "2023-01-04 12:39",
        "kpiValues": "{\"kpiThreshold\": 23, \"kpiValue\": 25, \"kpiDiff\": 2}"
    },
    {
        "id": 2,
        "generateddatetime": "2023-01-03 12:39",
        "kpiValues": "{\"kpiThreshold\": 23, \"kpiValue\": 26, \"kpiDiff\": 3}"
    },
    {
        "id": 3,
        "generateddatetime": "2023-01-02 12:39",
        "kpiValues": "{\"kpiThreshold\": 23, \"kpiValue\": 27, \"kpiDiff\": 4}"
    }
]

但我要的回应是

[
        {
            "id": 1,
            "generateddatetime": "2023-01-04 12:39",
            "kpiValues": {
                          "kpiThreshold": 23,
                          "kpiValue": 25,
                          "kpiDiff": 2
                         }
        },
        {
            "id": 2,
            "generateddatetime": "2023-01-03 12:39",
            "kpiValues": {
                          "kpiThreshold": 23,
                          "kpiValue": 26,
                          "kpiDiff": 3
                         }
        },
        {
            "id": 3,
            "generateddatetime": "2023-01-02 12:39",
            "kpiValues": {
                          "kpiThreshold": 23,
                          "kpiValue": 27,
                          "kpiDiff": 4
                         }
        }
    ]

有什么指点或建议我如何才能达到同样的效果?

tuwxkamq

tuwxkamq1#

可以更新序列化程序并尝试以下操作,

from rest_framework import serializers
from pegaapi.models import PegaAlerts
import json 

class AlertsSerializer(serializers.ModelSerializer):

    kpiValues = serializers.SerializerMethodField()

    def get_kpiValues(self, obj):
        return json.loads(obj.kpiValues)

    class Meta:
        model = PegaAlerts
        fields = ('id','generateddatetime','kpiValues')

相关问题