Skip to content

Instantly share code, notes, and snippets.

@akshar-raaj
Created August 7, 2019 13:18
Show Gist options
  • Select an option

  • Save akshar-raaj/e401d80d79d799c87368972ef283c4b4 to your computer and use it in GitHub Desktop.

Select an option

Save akshar-raaj/e401d80d79d799c87368972ef283c4b4 to your computer and use it in GitHub Desktop.
User serializer with validate_password
class UserSerializer(serializers.ModelSerializer):
def validate_password(self, value):
if value.isalnum():
raise serializers.ValidationError('password must have atleast one special character.')
return value
class Meta:
model = User
fields = ('username', 'email', 'first_name', 'last_name', 'password')
extra_kwargs = {
'password': {'write_only': True}
}
@jeffersfp
Copy link

Using validate_fieldname when serializer is a subclass of serializers.ModelSerializer seems to miss-validate of the field, falling back to the default validation of the ModelSerializer based on the model field attributes.

I had to implement a basic serializers.Serializer describing all the fields according to the model and then the implementation of thevalidate_fieldname methods did work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment