Created
August 7, 2019 13:26
-
-
Save akshar-raaj/b06edda6d74c8dd617404639b2ea024e to your computer and use it in GitHub Desktop.
User serializer with to_internal_value
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 | |
| 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 | |
| def to_internal_value(self, data): | |
| user_data = data['user'] | |
| return super().to_internal_value(user_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