Created
October 25, 2021 19:55
-
-
Save chapimenge3/d9d5eccc1639e47b51981b17d2ac05ad to your computer and use it in GitHub Desktop.
This file contains 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(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