Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

7
How to include Django's default password validators during User Registration in Django Rest Framework?
Post Body

I want to start by saying I am fairly new to Django Rest Framework. I have a fully functioning users app built in Django which has user registration, login, logout, password change and password reset all working well. I am trying to replicate these to have their own REST API endpoints using DRF.

I have created a custom user model as shown below. I have written a serializer to handle user registration as shown below. Everything works fine except that the default password validators which are in-built in Django seem to be missing. How can I enable it as it is readily available in settings.py? Any guidance on this would be greatly appreciated.

models.py:

class CustomUser(AbstractBaseUser):
    email = models.EmailField(unique=True, max_length=100)
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255, blank=True, default='')

serializers.py

class UserRegisterSerializer(serializers.ModelSerializer):
    # Define password fields so that text entered is not visible
    password = serializers.CharField(
        style={'input_type': 'password'},
        write_only=True,
        required=True,
    )
    password2 = serializers.CharField(
        style={'input_type': 'password'},
        write_only=True,
        required=True,
    )

    class Meta:
        model = CustomUser
        # Add fields which are to be filled during registration
        fields = ['id', 'email', 'first_name', 'last_name', 'password', 'password2']

    def create(self, validated_data):
        # Create a new user object
        user = CustomUser(
            email=self.validated_data['email'],
            first_name=self.validated_data['first_name'],
            last_name=self.validated_data['last_name'],
        )

        password = self.validated_data['password']
        password2 = self.validated_data['password2']

        # Check if passwords entered are matching. If not, raise error
        if password != password2:
            raise serializers.ValidationError({'password': 'Passwords must match.'})

        # Hash the password before it is saved
        user.set_password(password)
        # Save the user object
        user.save()
        return user

views.py:

class UserRegisterCreateAPIView(CreateAPIView):
    serializer_class = UserRegisterSerializer

Author
User Disabled
Account Strength
0%
Disabled 4 months ago
Account Age
5 years
Verified Email
Yes
Verified Flair
No
Total Karma
6,531
Link Karma
663
Comment Karma
5,737
Profile updated: 4 days ago
Posts updated: 1 year ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
4 years ago