How to override model field validation in django rest framework ModelSerializer How to override model field validation in django rest framework ModelSerializer django django

How to override model field validation in django rest framework ModelSerializer


remove unique constraint on mobile number of table,so django serializer will validate according to that .

or alternatively,

   serializer=UserProfileSerializer(data=request.DATA,partial=True)


I understand you won't save the serializer data. So, you can set mobileNumber as a read_only field on UserProfileSerializer.

Check serializer fields documentation for more info: http://www.django-rest-framework.org/api-guide/fields/#core-arguments


By overriding the model field within the serializer, and specifying required=False, allow_blank=True, allow_null=True:

class SomeModel(models.Model):   some_model_field_which_is_required = models.ForeignKey(...)   some_other_required_field = models.CharField(...)class SomeModelSerializer(serializers.ModelSerializer):    some_model_field_which_is_required = SomeNestedSerializer(        many=True, required=False, allow_blank=True    )    some_other_required_field = serializers.CharField(required=False, allow_blank=True)   def validate(self, *args, **kwargs):       print('should get here')   def validate_some_other_required_field(self, *args, **kwargs):       print('should also get here')    class Meta:        model = SomeModel