只在子级别显示某个序列化器字段(新手)

时间:2016-06-06 14:55:58

标签: python serialization django-rest-framework

我正在慢慢学习如何使用休息框架,而且我陷入了一个我不太了解的部分(我的英语也不是很好)。我有这个api点:我在api / building /上展示了一些数据,但是我希望某个字段只出现在api / building / 1(1 = pk number)上,我无法弄清楚如何。 这是我到目前为止的序列化程序代码:

class FloorSerializer(serializers.ModelSerializer):
    class Meta:
        model = Floor
        fields = ('number',
                  'title')


class BuildingSerializer(serializers.HyperlinkedModelSerializer):
    location = serializers.CharField(source='location_address')
    avg_temperature = serializers.SerializerMethodField('_get_avg_temperature')
    avg_humidity = serializers.SerializerMethodField('_get_avg_humidity')
    occupancy_level = serializers.SerializerMethodField('_get_occupancy_level')
    floors = FloorSerializer(many=True, read_only=True)
    class Meta:
        model = Building
        fields = ('pk',
                  'title',
                  'image_url',
                  'location',
                  'campus_name',
                  'avg_temperature',
                  'avg_humidity',
                  'occupancy_level',
                  'floors')

    def _get_avg_temperature(self, obj):
        # magia filtrului per buildingu asta.
        temp = SensorData.objects.filter(sensor__room__floor__building__pk=obj.pk).filter(sensor__type='TP')\
            .aggregate(Avg('value'))
        return temp

    def _get_avg_humidity(self, obj):
        # magia filtrului per buildingu asta.
        hum = SensorData.objects.filter(sensor__room__floor__building__pk=obj.pk).filter(sensor__type='HU')\
            .aggregate(Avg('value'))
        return hum

    def _get_occupancy_level(self, obj):
        ocup = randint(45, 65)
        return ocup

有问题的领域是地板。我想只在api / building / pk级别上显示它,当我阅读文档时,我不太清楚。

1 个答案:

答案 0 :(得分:0)

这是一个很好的答案,展示了你应该做的事情:

https://stackoverflow.com/a/22755648/2402929

总之,您应该创建一个序列化程序,它将包含您在列表路径中所需的所有方法和字段(/ api / building /),然后扩展该序列化程序,在详细路径中添加所需的其他字段(/ API /建筑/:PK)

示例:

class BuildingSerializer(serializers.HyperlinkedModelSerializer):

    # additional methods for fields that will be 
    # inherited in the DetailBuildingSerializer

    class Meta:
        model = Building
        fields = ('pk',
                  'title',
                  'image_url',
                  'location',
                  'campus_name',
                  'avg_temperature',
                  'avg_humidity',
                  'occupancy_level',
                  )

class DetailBuildingSerializer(BuildingSerializer):

    class Meta:
        model = Building
        fields = ('pk',
                  'title',
                  'image_url',
                  'location',
                  'campus_name',
                  'avg_temperature',
                  'avg_humidity',
                  'occupancy_level',
                  'floors'
                  )

稍后,视图集中的单独序列化程序(假设您正在使用视图集):

class BuildingViewset(viewsets.ModelViewSet):
    def get_serializer_class(self):
        if self.action == 'list':
            return serializers.BuildingSerializer
        if self.action == 'retrieve':
            return serializers.BuildingDetailSerializer