DjangoRestFramework does not check required = True

Today I have a very strange problem.

Here is my serializer class.

 class Connectivity(serializers.Serializer):

    device_type = serializers.CharField(max_length=100,required=True)
    device_name = serializers.CharField(max_length=100,required=True)

class Connections(serializers.Serializer):

    device_name = serializers.CharField(max_length=100,required=True)
    connectivity = Connectivity(required = True, many = True)


class Topologyserializer(serializers.Serializer):

    name = serializers.CharField(max_length=100,required=True, \
                                  validators=[UniqueValidator(queryset=Topology.objects.all())])
    json = Connections(required=True,many=True)

    def create(self, validated_data):
        return validated_data

I call Topologyserializerfrom a Django view, and I pass json as:

{

    "name":"tokpwol",
    "json": [

    ]
}

According to my experience with DRF, as I mentioned required = Truein the field json, it should not accept the above json.

But I can create a record. Can someone suggest me why it is not checking the json field and how it accepts an empty list as a json field?

I am using django rest framework 3.0.3

+4
source share
1 answer

DRF , required . , , , .

, , . , TopologySerializer:

def validate_json(self, value):
    if not value:
        raise serializers.ValidationError("Connections list is empty")
    return value

, .

+2

All Articles