I'm having trouble getting data in validate() function of Serializer for my API. I'm using django AbstractUser Model
Django = "^3.1.3"
djangorestframework = "^3.12.2"
My serializers.py:
class ChangePasswordSerializer(serializers.Serializer):
old_password = serializers.CharField(max_length=255, required=True)
new_password = serializers.CharField(max_length=255, required=True, write_only=True)
new_password_confirm = serializers.CharField(max_length=255, required=True, write_only=True)
def validate_old_password(self, value):
if not self.context['user'].check_password(value): # got data
raise serializers.ValidationError("Incorrect Old Password")
def validate_new_password(self, value):
try:
# validate the password and catch the exception
validators.validate_password(password=value) # got data
# the exception raised here is different than serializers.ValidationError
except exceptions.ValidationError as e:
raise serializers.ValidationError(list(e))
def validate_new_password_confirm(self, value):
try:
# validate the password and catch the exception
validators.validate_password(password=value) # got data
# the exception raised here is different than serializers.ValidationError
except exceptions.ValidationError as e:
raise serializers.ValidationError(list(e))
def validate(self, data):
if data['new_password'] != data['new_password_confirm']: # both return None
raise serializers.ValidationError({'message': ["Your password and confirmation password do not match."]})
return data
views.py:
class change_password(APIView):
def post(self, request):
received_json_data=request.data
user = request.user
serializer = ChangePasswordSerializer(data=received_json_data, context={'user': user})
if serializer.is_valid():
user.set_password(received_json_data['new_password']) # got new_password
return JsonResponse({
'message': 'Password changed.'
}, status=200)
else:
return JsonResponse({'message':serializer.errors}, status=400)
The problem is in validate(self, data) the data currently return as OrderedDict([('old_password', None), ('new_password', None), ('new_password_confirm', None)]) so it skipped the custom validation, but in other validation methods validate_old_password , validate_new_password and validate_new_password_confirm i got value normally when i print it out
I'm so confused on why this is happening