使用动态序列化器的 django rest 框架的问题

Issues with django rest framework using dynamic serializer

我有一个动态数据集,我正尝试通过动态创建序列化程序与 Django REST Framework 一起使用,但它似乎不处理我添加的动态属性。

代码如下:

-- views.py
class DynamicReadings(generics.ListAPIView):
    def get_serializer_class(self):
        site = self.kwargs['site']
        devices = Device.objects.filter(reader__site__slug=site).order_by('code')
        dyn_fields = ['x%s' % a.code for a in devices]
        return SerializerClassFactory(dyn_fields)

    def get_queryset(self):
        ...
        # the query is correct and returns data

-- serializers.py
def SerializerClassFactory(dyn_fields):
class DynamicSerializer(serializers.Serializer):
    read_at = serializers.DateTimeField()

    class Meta:
        fields = ('read_at')

    for f in dyn_fields:
        setattr(DynamicSerializer, f, serializers.FloatField())
    DynamicSerializer.Meta.fields += ','.join("'%s'" % f for f in dyn_fields)

    return DynamicSerializer

当我执行代码时,只有 class 定义中指定的属性 (read_at) 被序列化。 None 的动态属性似乎有效。

示例:

[{"read_at":"2017-05-07T00:12:29Z"},{"read_at":"2017-05-08T00:12:30Z"}]

这是 class 在 shell 中创建后的输出:

>>> serializer = SerializerClassFactory(['x01', 'x02', 'x03'])
>>> serializer.__dict__
mappingproxy({'__module__': 'xxx.api.serializers', 
'Meta': <class 'xxx.api.serializers.SerializerClassFactory.<locals>.DynamicSerializer.Meta'>, 
'x02': FloatField(), '_declared_fields': OrderedDict([('read_at', DateTimeField())]), 
'__doc__': None, 'x01': FloatField(), 'x03': FloatField()})

此外,我试过:

setattr(DynamicSerializer, f, property(serializers.FloatField()))
setattr(DynamicSerializer, f, type(serializers.FloatField()))

我不确定我做错了什么。

这里,只有字段是动态的,基础序列化器是一样的。为了让它工作,我们将只更改 DynamicSerializer

上的字段

serializers.py 你会:

class DynamicSerializer(serializers.Serializer):
    read_at = serializers.DateTimeField()

    class Meta:
        fields = ('read_at')

    def __init__(*args, dyn_fields=None, **kwargs):
        dyn_fields = dyn_fields or []
        for field in dyn_fields:
            self.fields[field] = serializers.FloatField()

        super(DynamicSerializer, self).__init__(*args, **kwargs)

并且在 views.pyget_serializer_class 方法中你会 return DynamicSerializer(dyn_fields=dyn_fields)

有关详细信息,请访问 http://www.django-rest-framework.org/api-guide/serializers/#dynamically-modifying-fields