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 ?
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
Most helpful comment
extra_kwargs: {'name': 'validators': []}means you'll override anyvalidators=...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....