Skip to content

Instantly share code, notes, and snippets.

@eugena
Created September 28, 2015 11:33
Show Gist options
  • Save eugena/128aa896fcea81af7743 to your computer and use it in GitHub Desktop.
Save eugena/128aa896fcea81af7743 to your computer and use it in GitHub Desktop.
class PasswordSerializer(serializers.ModelSerializer):
"""
Changes user password
"""
old_password = serializers.CharField(required=True)
new_password = serializers.CharField(required=True)
_required_fields = ['old_password', 'new_password']
def validate_old_password(self, value):
"""
Validates that the old_password field is correct.
"""
if not self.instance.check_password(value):
raise serializers.ValidationError(
"Your old password was entered incorrectly. Please enter it again.")
return value
def validate_new_password(self, value):
"""
Validates that the old_password field is correct.
"""
self.instance.set_password(value)
return value
class Meta():
model = Client
fields = ['old_password', 'new_password', ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment