django - Validate specific field in DRF serializer -


i have model jsonfield.

model.py

class categories(models.model):      type = models.charfield(max_length=20)      name = models.charfield(max_length=500)      details = jsonfield(blank=true, null=true) 

currently i'm using serializers.modelserializer serializing above model:

serializers.py

class categoriesserializer(serializers.modelserializer):      class meta:          model = categories          fields = ('id', 'type', 'name', 'details') 

due this, details field checked contain valid json. need perform custom validation based on json schema defined details field. since don't want other custom validations rest of fields, keep using validations provided serializers.modelserializer. there way can override validation 1 field, writing custom serializer for details field?

note question not how write custom validator, how use custom validator on field in serializer inheriting modelserializer

drf's serializers provide field level validation option. can perform details field validation implementing validate_details method:

class categoriesserializer(serializers.modelserializer):      class meta:          model = categories          fields = ('id', 'type', 'name', 'details')       def validate_details(self, value):         if value['not_valid']:             raise serializers.validationerror("not valid")         return value 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -