Skip to content

Instantly share code, notes, and snippets.

@mitchtabian
Created October 15, 2019 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitchtabian/5dce50c510e9eff8ecf531c8a4f63bf5 to your computer and use it in GitHub Desktop.
Save mitchtabian/5dce50c510e9eff8ecf531c8a4f63bf5 to your computer and use it in GitHub Desktop.
from rest_framework import serializers
from account.models import Account
class RegistrationSerializer(serializers.ModelSerializer):
password2 = serializers.CharField(style={'input_type': 'password'}, write_only=True)
class Meta:
model = Account
fields = ['email', 'username', 'password', 'password2']
extra_kwargs = {
'password': {'write_only': True},
}
def save(self):
account = Account(
email=self.validated_data['email'],
username=self.validated_data['username']
)
password = self.validated_data['password']
password2 = self.validated_data['password2']
if password != password2:
raise serializers.ValidationError({'password': 'Passwords must match.'})
account.set_password(password)
account.save()
return account
class AccountPropertiesSerializer(serializers.ModelSerializer):
class Meta:
model = Account
fields = ['pk', 'email', 'username', ]
class ChangePasswordSerializer(serializers.Serializer):
old_password = serializers.CharField(required=True)
new_password = serializers.CharField(required=True)
confirm_new_password = serializers.CharField(required=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment