Created
August 7, 2019 13:18
-
-
Save akshar-raaj/e401d80d79d799c87368972ef283c4b4 to your computer and use it in GitHub Desktop.
User serializer with validate_password
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using
validate_fieldnamewhen serializer is a subclass ofserializers.ModelSerializerseems 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.Serializerdescribing all the fields according to the model and then the implementation of thevalidate_fieldnamemethods did work.