Skip to content

Instantly share code, notes, and snippets.

@chapimenge3
Created October 25, 2021 19:55
Show Gist options
  • Save chapimenge3/d9d5eccc1639e47b51981b17d2ac05ad to your computer and use it in GitHub Desktop.
Save chapimenge3/d9d5eccc1639e47b51981b17d2ac05ad to your computer and use it in GitHub Desktop.
class UserSerializer(ModelSerializer):
password = CharField(style={'input_type': 'password'}, write_only=True)
password2 = CharField(style={'input_type': 'password'}, write_only=True)
class Meta:
model = get_user_model()
fields = ('id', 'username', 'password', 'password2',
'email', 'first_name', 'last_name',)
extra_kwargs = {
'first_name': {'required': True},
'last_name': {'required': True},
'password': {'write_only': True},
'password2': {'write_only': True},
}
def create(self, validated_data):
'''
Override the create method of this to create user
'''
user = get_user_model().objects.create(
username=validated_data['username'],
email=validated_data['email'],
first_name=validated_data['first_name'],
last_name=validated_data['last_name']
)
# validate password and set password
user.set_password(validated_data['password'])
user.save()
return user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment