Created
August 7, 2019 13:21
-
-
Save akshar-raaj/f8a54e5791113c34059d4e8462a1d6e7 to your computer and use it in GitHub Desktop.
User serializer with validate()
This file contains 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 | |
def validate(self, data): | |
if data['first_name'] == data['last_name']: | |
raise serializers.ValidationError("first_name and last_name shouldn't be same.") | |
return data | |
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