Django序列化程序

时间:2017-07-15 10:45:04

标签: django serialization django-rest-framework

我希望收到格式化的JSON:

{
    detailing: [
        <detailing>,
        ...
    ],
    results: [
        {
            id: <id>,
            data: [
                {
                    x: <tag>,
                    y: <count>
                },
                ...
            ]
            summ: <summary count>,
            percentage: <percentage summary count>
        },
        {
            id: <id>,
            data: [
                {
                    x: <tag>,
                    y: <count>
                },
                ...
            ]
            summ: <summary count>,
            percentage: <percentage summary count>
        },
        ...
    ]
}

换句话说,我需要详细说明&#39;(不是针对所有&#39; Floor&#39;实例,但对所有人而言)字段超过&#39;结果&#39;字段和&#39;结果&#39;必须包含所有&#39; Floor&#39;实例。我认为,对于这个问题解决,我需要3个序列化器类,一个用于&#39;详细说明&#39;一个用于&#39;结果&#39;和一个聚合在一起。

这是我的代码示例:

#  serializers.py
class DetailingSerializer(serializers.Serializer):
    detailing = serializers.SerializerMethodField()

    class Meta:
        fields = ('detailing',)

    def get_detailing(self, obj):
        return ['dekaminute', 'hour', 'day']

class FloorTrackPointsCountSerializer(serializers.ModelSerializer):
    results = serializers.SerializerMethodField()
    class Meta:
        model = Floor
        fields = ('results','detailing',)

    def get_results(self, obj):
        res = [
            {
                'id': obj.id,
                'data': [{'tag':tp.tag,'timestamp':tp.created_at} for tp in obj.trackpoint_set.all()],
                'summ': obj.trackpoint_set.values('tag').distinct().count(),
                'percentage': 0.5
            }

        ]
        return res

class AggregationSerializer(serializers.Serializer):
    detailing = DetailingSerializer(many=True)
    results = FloorTrackPointsCountSerializer(many=True)

    class Meta:
        fields = ('results', 'detailing')


#  view.py
class FloorTrackPointsCountViewSet(ListForGraphicViewSet):
    queryset = Floor.objects.all()
    serializer_class = AggregationSerializer

#  models.py
class Floor(InModel):
    name = models.CharField(max_length=1000, null=False, blank=True)
    number = models.PositiveSmallIntegerField(null=True, blank=True)
    building = models.ForeignKey(Building)

    class Meta:
        ordering = ['number']

class TrackPoint(VirtualDestroyModel):
    tag = models.CharField(max_length=100)
    floor = models.ForeignKey(Floor)
    x                       = models.FloatField(default=0, null=False, blank=False)
    y                       = models.FloatField(default=0, null=False, blank=False)
    def __str__(self):
        return self.tag

现在,我所拥有的只是Traceback

AttributeError: Got AttributeError when attempting to get a value for field `detailing` on serializer `AggregationSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `Floor` instance.
Original exception text was: 'Floor' object has no attribute 'detailing'.

我的错误是什么,这种做法是否合理? 最诚挚的问候。

0 个答案:

没有答案