How to dynamically change depth in Django Rest Fra

2019-04-29 00:11发布

问题:

I have a set of nested serializers which have a depth set on their respective Meta classes. I'd like to programmatically change the depth based on parameters passed into in views.

class ResourceSerializer(serializers.ModelSerializer):
    type         = serializers.PrimaryKeyRelatedField(queryset=EntityType.objects.all())
    tags         = serializers.PrimaryKeyRelatedField(queryset=Tag.objects.all(), many=True)

    class Meta:
        model  = Resource
        fields = ('id', 'type', 'uri', 'tags', 'created_date')
        depth = 1

Unfortunately, there doesn't seem to be a way to override the depth attribute at runtime. My current solution has been to inherit the "shallow" serializers and override their Meta classes to adjust the depth.

class ResourceNestedSerializer(ResourceSerializer):
    class Meta(ResourceSerializer.Meta):
        depth = 2

And in my view:

    if nested:
        serializer = ContainerNestedSerializer(containers, many=True)
    else:
        serializer = ContainerSerializer(containers, many=True)
    return Response(serializer.data)

Is there any way to adjust depth before calling serializer.data?

回答1:

Here is my code that incorporates including/excluding fields, as well as dynamically adjusting the depth. Adjust it to your taste. :)

class DynamicModelSerializer(serializers.ModelSerializer):
"""
A ModelSerializer that takes an additional `fields` argument that
controls which fields should be displayed, and takes in a "nested"
argument to return nested serializers
"""

def __init__(self, *args, **kwargs):
    fields = kwargs.pop("fields", None)
    exclude = kwargs.pop("exclude", None)
    nest = kwargs.pop("nest", None)

    if nest is not None:
        if nest == True:
            self.Meta.depth = 1

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

    if fields is not None:
        # Drop any fields that are not specified in the `fields` argument.
        allowed = set(fields)
        existing = set(self.fields.keys())
        for field_name in existing - allowed:
            self.fields.pop(field_name)

    if exclude is not None:
        for field_name in exclude:
            self.fields.pop(field_name)