在 Django Rest 框架中返回序列化器数据时出错

问题描述 投票:0回答:1

当我尝试使用此代码发送数据时:

class MeterData1(APIView):
    def get(self, request, formate=None):
        queryset = PowerMeter.objects.all(id=10)
        serializer = PowerMeterSerializer(data=queryset,many=True)
        if serializer.is_valid():
            return Response(serializer.data, status=status.HTTP_200_OK)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

我遇到这个错误:

{
    "non_field_errors": [
        "Invalid data. Expected a dictionary, but got QuerySet."
    ]
}

这是我的序列化器:

class PowerMeterSerializer(serializers.ModelSerializer):
    class Meta:
        model = PowerMeter
        fields = '__all__'

及型号:

class PowerMeter(models.Model):
    meter_id = models.CharField(max_length=127)
    State = models.ForeignKey(State, on_delete=models.CASCADE)
    date = models.DateTimeField(auto_now=True, blank=True)

    VII1 = models.PositiveIntegerField(default=0, blank=True)
    VII2 = models.PositiveIntegerField(default=0, blank=True)
    VII3 = models.PositiveIntegerField(default=0, blank=True)
    VII_avg = models.PositiveIntegerField(default=0, blank=True)

    Vln1 = models.PositiveIntegerField(default=0, blank=True)
    Vln2 = models.PositiveIntegerField(default=0, blank=True)
    Vln3 = models.PositiveIntegerField(default=0, blank=True)
    Vln_avg = models.PositiveIntegerField(default=0, blank=True)

    I1 = models.PositiveIntegerField(default=0, blank=True)
    I2 = models.PositiveIntegerField(default=0, blank=True)
    I3 = models.PositiveIntegerField(default=0, blank=True)
    I_avg = models.PositiveIntegerField(default=0, blank=True)

    P1 = models.PositiveIntegerField(default=0, blank=True)
    P2 = models.PositiveIntegerField(default=0, blank=True)
    P3 = models.PositiveIntegerField(default=0, blank=True)
    P_total = models.PositiveIntegerField(default=0, blank=True)

    Q1 = models.PositiveIntegerField(default=0, blank=True)
    Q2 = models.PositiveIntegerField(default=0, blank=True)
    Q3 = models.PositiveIntegerField(default=0, blank=True)
    Q_total = models.PositiveIntegerField(default=0, blank=True)

    S1 = models.PositiveIntegerField(default=0, blank=True)
    S2 = models.PositiveIntegerField(default=0, blank=True)
    S3 = models.PositiveIntegerField(default=0, blank=True)
    S_total = models.PositiveIntegerField(default=0, blank=True)

    PF1 = models.PositiveIntegerField(default=0, blank=True)
    PF2 = models.PositiveIntegerField(default=0, blank=True)
    PF3 = models.PositiveIntegerField(default=0, blank=True)
    PF_totalMax_Demand_import = models.PositiveIntegerField(default=0, blank=True)

    Max_Demand_export = models.PositiveIntegerField(default=0, blank=True)
    Current_Demand = models.PositiveIntegerField(default=0, blank=True)
    Frequency = models.PositiveIntegerField(default=0, blank=True)
    Unb_V = models.PositiveIntegerField(default=0, blank=True)
    Unb_I = models.PositiveIntegerField(default=0, blank=True)
    In = models.PositiveIntegerField(default=0, blank=True)

我设法使用“ListCreateAPIView”获取数据,但由于某种原因我不想使用它

我尝试过:

 serializer = PowerMeterSerializer(data=queryset)
但得到:

{
    "non_field_errors": [
        "Invalid data. Expected a dictionary, but got QuerySet."
    ]
}

当我打印“serializer.data”时,我发现它不是数据的一部分,这里是数据:

[OrderedDict([('id', 6), ('meter_id', '323123'), ('date', '2023-12-13T15:35:23.767942Z'), ('VII1', 220), ('VII2', 220), ('VII3', 220), ('VII_avg', 220), ('Vln1'
, 65), ('Vln2', 65), ('Vln3', 65), ('Vln_avg', 65), ('I1', 10), ('I2', 10), ('I3', 10), ('I_avg', 10), ('P1', 10), ('P2', 10), ('P3', 10), ('P_total', 10), ('Q1
', 23), ('Q2', 20), ('Q3', 32), ('Q_total', 54), ('S1', 32), ('S2', 35), ('S3', 66), ('S_total', 66), ('PF1', 88), ('PF2', 88), ('PF3', 88), ('PF_totalMax_Deman
d_import', 89), ('Max_Demand_export', 90), ('Current_Demand', 35), ('Frequency', 22), ('Unb_V', 3), ('Unb_I', 10), ('In', 10), ('State', 2)]), OrderedDict([('id
', 7), ('meter_id', '313123'), ('date', '2023-12-13T15:37:42.712157Z'), ('VII1', 12), ('VII2', 12), ('VII3', 12), ('VII_avg', 15), ('Vln1', 41), ('Vln2', 41), (
'Vln3', 41), ('Vln_avg', 15), ('I1', 35), ('I2', 35), ('I3', 35), ('I_avg', 85), ('P1', 12), ('P2', 12), ('P3', 12), ('P_total', 15), ('Q1', 15), ('Q2', 16), ('
Q3', 18), ('Q_total', 18), ('S1', 18), ('S2', 14), ('S3', 71), ('S_total', 65), ('PF1', 31), ('PF2', 32), ('PF3', 46), ('PF_totalMax_Demand_import', 76), ('Max_
8), ('Unb_V', 98), ('Unb_I', 65), ('In', 65), ('State', 2)])]

但应该是这样的:

[OrderedDict([('id', 6), ('meter_id', '323123'), ('date', '2023-12-13T15:35:23.767942Z'), ('VII1', 220), ('VII2', 220), ('VII3', 220), ('VII_avg', 220), ('Vln1'
, 65), ('Vln2', 65), ('Vln3', 65), ('Vln_avg', 65), ('I1', 10), ('I2', 10), ('I3', 10), ('I_avg', 10), ('P1', 10), ('P2', 10), ('P3', 10), ('P_total', 10), ('Q1
', 23), ('Q2', 20), ('Q3', 32), ('Q_total', 54), ('S1', 32), ('S2', 35), ('S3', 66), ('S_total', 66), ('PF1', 88), ('PF2', 88), ('PF3', 88), ('PF_totalMax_Deman
d_import', 89), ('Max_Demand_export', 90), ('Current_Demand', 35), ('Frequency', 22), ('Unb_V', 3), ('Unb_I', 10), ('In', 10), ('State', 2)]), OrderedDict([('id
', 7), ('meter_id', '313123'), ('date', '2023-12-13T15:37:42.712157Z'), ('VII1', 12), ('VII2', 12), ('VII3', 12), ('VII_avg', 15), ('Vln1', 41), ('Vln2', 41), (
'Vln3', 41), ('Vln_avg', 15), ('I1', 35), ('I2', 35), ('I3', 35), ('I_avg', 85), ('P1', 12), ('P2', 12), ('P3', 12), ('P_total', 15), ('Q1', 15), ('Q2', 16), ('
Q3', 18), ('Q_total', 18), ('S1', 18), ('S2', 14), ('S3', 71), ('S_total', 65), ('PF1', 31), ('PF2', 32), ('PF3', 46), ('PF_totalMax_Demand_import', 76), ('Max_
Demand_export', 32), ('Current_Demand', 71), ('Frequency', 92), ('Unb_V', 85), ('Unb_I', 67), ('In', 10), ('State', 3)]), OrderedDict([('id', 8), ('meter_id', '
132031\n'), ('date', '2023-12-13T15:43:25.306111Z'), ('VII1', 18), ('VII2', 19), ('VII3', 13), ('VII_avg', 71), ('Vln1', 16), ('Vln2', 64), ('Vln3', 91), ('Vln_
avg', 37), ('I1', 85), ('I2', 73), ('I3', 45), ('I_avg', 48), ('P1', 56), ('P2', 12), ('P3', 19), ('P_total', 20), ('Q1', 30), ('Q2', 40), ('Q3', 50), ('Q_total
', 60), ('S1', 74), ('S2', 61), ('S3', 13), ('S_total', 40), ('PF1', 19), ('PF2', 14), ('PF3', 14), ('PF_totalMax_Demand_import', 54), ('Max_Demand_export', 75)
, ('Current_Demand', 51), ('Frequency', 87), ('Unb_V', 13), ('Unb_I', 74), ('In', 52), ('State', 2)]), OrderedDict([('id', 9), ('meter_id', '131531'), ('date', 
'2023-12-13T15:46:57.291928Z'), ('VII1', 16), ('VII2', 14), ('VII3', 15), ('VII_avg', 54), ('Vln1', 75), ('Vln2', 12), ('Vln3', 13), ('Vln_avg', 12), ('I1', 48)
, ('I2', 14), ('I3', 12), ('I_avg', 31), ('P1', 45), ('P2', 87), ('P3', 54), ('P_total', 17), ('Q1', 45), ('Q2', 54), ('Q3', 13), ('Q_total', 74), ('S1', 64), (
'S2', 17), ('S3', 34), ('S_total', 76), ('PF1', 34), ('PF2', 46), ('PF3', 12), ('PF_totalMax_Demand_import', 43), ('Max_Demand_export', 43), ('Current_Demand', 
75), ('Frequency', 24), ('Unb_V', 73), ('Unb_I', 74), ('In', 43), ('State', 3)]), OrderedDict([('id', 10), ('meter_id', '221316'), ('date', '2023-12-13T15:49:39
.938396Z'), ('VII1', 15), ('VII2', 65), ('VII3', 95), ('VII_avg', 47), ('Vln1', 32), ('Vln2', 48), ('Vln3', 74), ('Vln_avg', 64), ('I1', 14), ('I2', 54), ('I3',
 42), ('I_avg', 21), ('P1', 21), ('P2', 12), ('P3', 46), ('P_total', 24), ('Q1', 17), ('Q2', 23), ('Q3', 32), ('Q_total', 5), ('S1', 54), ('S2', 546), ('S3', 65
), ('S_total', 56), ('PF1', 56), ('PF2', 56), ('PF3', 98), ('PF_totalMax_Demand_import', 54), ('Max_Demand_export', 56), ('Current_Demand', 54), ('Frequency', 7
8), ('Unb_V', 98), ('Unb_I', 65), ('In', 65), ('State', 2)])]
python django serialization django-rest-framework django-serializer
1个回答
0
投票

不应该是

data=…
data=…
是当你以相反的方式使用序列化器时:从客户端的数据生成对象,所以:

serializer = PowerMeterSerializer(queryset, many=True)

或者如果您想使用

named
参数,则作为 instance=…

serializer = PowerMeterSerializer(instance=queryset, many=True)

注意:请不要在模型中存储聚合:在需要时确定聚合:在模型中存储聚合会使更新和保持数据同步变得更加困难。

© www.soinside.com 2019 - 2024. All rights reserved.