Django-rest-framework: model serialiser not respecting validators specified in extra_kwargs

Created on 3 Oct 2017  路  2Comments  路  Source: encode/django-rest-framework

This is my serializer.py

class CostSerializer(serializers.ModelSerializer):
    def validate(self, attrs):
        # do some validations on initial_data
        return self.initial_data

   class Meta:
        model = Cost
        fields = ['name']
        extra_kwargs = {'name': {'validators': []}}

This is my models.py

class Cost(models.Model):
    name = models.CharField(max_length=255)

Why is the model serialiser not respecting the validators (empty validation) which I specified in extra_kwargs ?

Most helpful comment

extra_kwargs: {'name': 'validators': []} means you'll override any validators=... argument that's present on the model. You won't be overriding any other arguments that are present (eg. max_length.)

If you want a field without any validation at all I'd suggest including the field explicitly on the serializer instead, eg....

name = serializers.CharField(...)

All 2 comments

extra_kwargs: {'name': 'validators': []} means you'll override any validators=... argument that's present on the model. You won't be overriding any other arguments that are present (eg. max_length.)

If you want a field without any validation at all I'd suggest including the field explicitly on the serializer instead, eg....

name = serializers.CharField(...)

Thanx mate. That works

Was this page helpful?
0 / 5 - 0 ratings